samedi 2 juillet 2016

What is the best algorithm or pattern for a small polling mechanism

I'm writing a small polling for products,which product could be polled by each user in 5 scores (which can increase later) for each of its attributes(like: Cleanliness and neatness , Services , location , Staff ). each score has an adjective (1:the worst, 2:bad , 3:good , 4:very good , 5:extraordinary).

for example a user can poll to one of the product like this: CleanLiness and Neatness : 4 (very good) Services : 3 (good) location : 1 (the worst) Staff : 5 (extraordinary)

the average of this scores would be the score of product, and it will be decimal, for this example , it is 3.25 .

now i want to give a adjective to the product by this result(3.25), if it's point is under of the half like 3.25, it rounds to down( for this 3) and if it's point is equal and above the half like 3.7, it rounds to up( 4)

I'm wonder what is the best algorithm or pattern for this ?

my classs' design is like below:

public class Product
{}

public Class Poll
{
  public int Id {get; set;}
  public int ProductId {get; set;}
  public Product Product {get; set;}
  public decimal Score {get; set}
  public string Adjective {get; set;}
  public ICollection<PollAttributes> Attributes {get; set;}

}

public class Attribute  // for the attribute like Services 
{
  public int Id {get; set;}
  public string Title {get; set;}
  public ICollection<PollAttributes> Attributes {get; set;}
}

public Class PollAttributes
{
  public decimal score {get; set;}

  public int   AttributeId {get; set;}
  public Attribute{get; set;}

  public int   PollId {get; set;} 
  public Poll Poll {get; set;} 
}

vendredi 1 juillet 2016

C64 Recognize common patterns between disk images

I have 2 disk images from the C64 for example:

  • disk1 it's a game and it starts with some splash-screen and the music.
  • diks2 it's a demo and contains many songs and the same music as the first disk.

Is it any chance that examining the 2 images with some Hex editor I can see the same pattern binary sequence at some point in the 2 files? Will be they stored in the same way? And if yes what would be the right approach to match the pattern?

(the goal is to search for that song in 150k+ program files and see in what disk it is used)

how do you implement classes with property subsets?

I have 3 classes ...

class1 {
  constructor(a, b, c) {
    this.a = a;
    this.b = b;
    this.c = c;
    this.toClass2 = function() {
      // TODO: return this as an instance of class2;
      // the conversion would remove the unwanted 'b' property
    }
    this.toClass3 = function() {
      // TODO: return this as an instance of class3;
      // the conversion would remove the unwanted 'a' property
    }
  }
}

class2 {
  constructor(a, c) {
    this.a = a;
    this.c = c;
  }
}

class3 {
  constructor(b, c) {
    this.b = b;
    this.c = c;
  }
}

The following statements are true ...

  • class1 could extend class2
  • class1 could extend class3
  • class1 could NOT extend class2 AND class3 (multiple inheritance not supported in JavaScript and multiple inheritance would give the derived class 4 properties, but I only want 3)

  • class2 has a subset of class1's properties

  • class3 has a subset of class1's properties

QUESTION: How can I best implement the classes in JavaScript or TypeScript so that the toClass2 and toClass3 conversion methods work? Are there any design patterns for this? Thank you

Failing to reset one of my object property, revealing module pattern

I was trying to solve that problem : http://ift.tt/29aDFLm but I'm stuck on the last requirement. Let me copy the problem

Write a program that manages robot factory settings.

When robots come off the factory floor, they have no name.

The first time you boot them up, a random name is generated, such as RX837 or BC811.

Every once in a while we need to reset a robot to its factory settings, which means that their name gets wiped. The next time you ask, it will respond with a new random name.

The names must be random: they should not follow a predictable sequence. Random names means a risk of collisions. Your solution should not allow the use of the same name twice when avoidable. In some exercism language tracks there are tests to ensure that the same name is never used twice.

This is the test suite : http://ift.tt/29guRpN. I manage all of the tests except the 2 last ones (related to reset).

My code in revealing module pattern

nameDb = {};

function Robot () {
  var name = '';

  var reset = function() {
    name = 'new name';
  }

  var randomizeLetter = function() {
    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var charToChose = randomizeNumber(1, 26);
    return chars[charToChose - 1];
  };

  var randomizeNumber = function(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  };

  var nameGenerator = function() {
    var min = 100;
    var max = 999;
    var output = '';

    output += randomizeLetter();
    output += randomizeLetter();
    output += randomizeNumber(min, max);
    return output;
  };

  // Called immediately
  var newRobot = function newRobot() {
    var candidat = nameGenerator();

    if(candidat in nameDb) {
      newRobot();
      return;
    } else {
      nameDb[candidat] = true;
      name += candidat;
    }
  }();

  return {
    name: name,
    reset: reset
  };
}

module.exports = Robot;

And this is the first failing test

  it('is able to reset the name', function() {
    var originalName = robot.name;
    robot.reset();
    var newName = robot.name;
    expect(newName).toMatch(/^[A-Z]{2}\d{3}$/);
    expect(originalName).not.toEqual(newName);
  });

Which returns Expected 'ZC189' not to equal 'ZC189'(for instance).

I'm not familiar with that pattern so I'm sure there is something I overlooked. Basically, my reset function does nothing, for some reason it's not able to actually set the name var to zero.

What I was trying to do is : when the object is first invoked it has a name property. When the reset is called the name property is set to 0 (or whatever). Then when the name is called again I give a new one (which must be unique to that object instance).

I guess the problem lies somewhere with my IIFE.. which I guess is not really good in a module pattern. Any insights/leads appreciated

Object oriented design pattern for parsing json files

I have sets of json files , each set having different structure .Each json structure have three things , either its a leaf or array or someOtherStructure .I want to write a generic parser which could parse a json for all sets of json files based on the above mentioned 3 structures.

Any anyone suggest a Object oriented design pattern for the above case.

lookup map values and calling matched implementations

I have the following design of my classes:

public interface Device {
    void create();
}

public class Printer implements Device {
    @Override
    public void create() {
}

public class Mouse implements Device {
    @Override
    public void create() {
}



public class ProcessImpl implements Processor {

   private final Map<String, Set<String>> deviceMap = new HashMap<>();

   @Override
   public void process(String input) {
       populateMap(input);

       //call to `provision(deviceMap)`

}

deviceMap values are arranged like this for example: {123=[P, M], 224=[C, P, M]}

where 123 is an ID and String values C',P,M` represents devices to be created against the corresponding ID.

My question is, how can I iterate over this map in provision(deviceMap) method and create the devices by calling their corresponding create() method. (achieving polymorphism) e.g.

When P -> Call create() in Printer implementation of Device. What is good design/pattern I can use to structure the classes? Can enum help with this or is there a better cleaner approach ?

What Design Pattern can be used here?

I am new to Design pattern. Can anybody help me to identify which design pattern can be used here ? please provide reason also.

Assume that we would like to model a personal computer (PC). A PC consists of a cabinet, which includes a chassis. A chassis on its turn is composed of a bus, a floppy disk drive, a memory unit, a CPU, and a power supply. A bus incorporates a network card. We would like to treat all the equipment components in a uniform way. The basic operations needed are netPrice and powerConsumption, which return the net price and the power consumption of each component respectively. In addition, each component may have its own specific semantics.