Language Gallery

This is a nice little real-world problem that I came across one day. I was so impressed with the Python solution that I challenged the C# programmers to match it. They did.

I thought it would be interesting to compare the various languages that people use by way of this challenge.

A class for storing a number of integers and real numbers. Each instance of the class can have a different number of ints and floats in any combination. For example: 3 ints, a float and another int.

The instance creation must be simple and no more than one line of code.
For example: lp = ListParams([int, int, int, float, int], [1, 2, 3, 4.4, 5])

The API should offer the following methods:

Accessor functions to read and write each of the stored numbers as numbers - int or float.
For example: lp[2] = lp[4]

An accessor function to set the numbers from a space-delimited string
For example: lp.set("9 8 7 6.6 5")

An accessor function to get the list of numbers as a space delimited string
For example: print lp.put # prints 9 8 7 6.6 5

Here is the Python solution:
Code: class ListParams(object):
   """Provides a data store with get and set methods.
   To interface the GUI to the data read and written by the protocol handlers,
   we need an object which provides get and set methods. Since the commands have
   their data in lists, we save it as such here too.
   The only fly in the ointment is that we want to save the data as numbers and
   send and receive it as strings.
   def __init__(self, fmt=[int, int, int], value = [0, 0, 0]):
      """Defines a data store containing a list of the given types.
      The fmt list is a list of types. The value list is a list of values.
      They should match up.
      self.fmt = fmt
      self.value = value
   def put(self, str):
      """Write a new value into the data store.
      We check that the right number of strings has been given, log an
      error if it hasn't and refuse to set the value.
      The code that stores the data makes using Python worthwhile. How neat is that?
      lst = str.split()

      if len(lst) != len(self.fmt):
         print "Wrong number of members in ListParams.FromString;", en(lst), "supplied, should be", len(self.fmt)
         return False

      self.value = map(lambda f,x: f(x), self.fmt, lst)

      return True
   def get(self):
      """Get the value as a single string.
      r =  " ".join(map(str, self.value))
      return r

   def __getitem__(self,i):
      """ Accessor for rhs expression in array [] form.
      return self.value[i]
   def __setitem__(self, i, v):
      """ Accessor for lhs expression in array [] form.

      Uses the format to cast the value in case the wrong type has been passed.
      self.value[i] = self.fmt[i](v)

Tagged as: language gallery

Probable answsers

wat program u usE?

hi Is there supposed to be a question or this just one of those misplaced informational posts? Good Guidelines For non-English speaking people here? Sticky ?.......... ...

that will be great thanks for updating us

Yeah that thread is a great idea. It comes down to difficulty, but the easier, less structured languages lead to bad habits that a programmer can pick up. Eg I do VB.Net in college which makes me forget to put semicolons ...

I am thinking strange we don't have our forum till now. Well, in the old My Opera foruns, the portuguese forum also took some time to be created, so i guess it's just a matter of hava patience.

Similair questions

You have a problem or a question but perhaps english is not a very good language for you to make yourself understood in. Try this: Post your question in the best possible english that you ...

Well, i am opening this topic as an official request for the (re)creation of the portuguese language forum. The old one was kinda active and people seems to be missing it. If you are in favor of that, please ...

Hey Guys,I've set up a YouTube channel where I plan to make tutorials on Linux, configuring the Raspberry Pi and Programming. More on that here ...

I just wanted to let everyone know that one of my most important goals for this forum is to bring back the popular gallery (and Downloads) section we once had before (at MDCF/ATRACLife). I know that many of ...

All All photos shot with a Canon D30 and tweaked in Photoshop. Let me know what you think. Rick Murtha