Author Topic: ( Challenge ) StringToBinary  (Read 10569 times)

0 Members and 1 Guest are viewing this topic.

SomeCallMeDave

  • Guest
Re: ( Challenge ) StringToBinary
« Reply #15 on: March 20, 2009, 10:01:11 PM »
But in Ruby you don't need a function,  unless you just want one

Code: [Select]
def s_to_bin(s1)
  s1.each_byte {|x| puts x.to_s(2).rjust(8,"0")}
end

a1="ABC"
a1.each_byte {|x| puts x.to_s(2).rjust(8,"0")}
puts "***"
s_to_bin(a1)
puts "****"
"ABC".each_byte {|x| puts x.to_s(2).rjust(8,"0")}


All return the same thing
Code: [Select]
01000001
01000010
01000011
***
01000001
01000010
01000011
****
01000001
01000010
01000011



**Edit.  Doh, Function.  So you can use the return value, right?  I remember functions from somewhere.  Stand by one.
« Last Edit: March 20, 2009, 10:05:48 PM by David Blackmon »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #16 on: March 20, 2009, 10:08:32 PM »
As demonstrated, Python didn't require a function def, but again to illuminate:

s = "ABC"

list(map(lambda a: (('0'*8)+bin(ord(a))[2:])[-8:],s))

=> ['01000001', '01000010', '01000011']

Having said that, I generally prefer functions, as they improve read-ability of the code.

Glad you're digging Ruby. I studied it for quite awhile but I'm preferring Python.

PS: You didn't return a list.

:P
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

SomeCallMeDave

  • Guest
Re: ( Challenge ) StringToBinary
« Reply #17 on: March 20, 2009, 10:11:00 PM »
Yea,  I had a momentary brain freeze

Code: [Select]
def s_to_bin(s1)
  retVal = Array.new
  s1.each_byte {|x| retVal.push( x.to_s(2).rjust(8,"0"))}
  retVal
end

a1="ABC"

x = s_to_bin(a1)
puts x.inspect


Will return
Code: [Select]
["01000001", "01000010", "01000011"]

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #18 on: March 20, 2009, 10:12:04 PM »
Good job.

PS: not so succinct now. :P

Sorry, had to. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

SomeCallMeDave

  • Guest
Re: ( Challenge ) StringToBinary
« Reply #19 on: March 20, 2009, 10:17:10 PM »
Give me a minute,  I'm still learning   :-D

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #20 on: March 20, 2009, 10:18:02 PM »
You and me both.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

SomeCallMeDave

  • Guest
Re: ( Challenge ) StringToBinary
« Reply #21 on: March 20, 2009, 10:29:39 PM »
Well,  the only way that I see to make it anymore succint is to shorten the variable names or use a smaller font,  but I'm not sure that really helps matters   :lmao:

But the fact that I can't see anyway should in no way be taken to mean that there isn't a way to do it.


Michael,  why Python over Ruby? (if I may ask without hijacking your thread).  I don't have enough time into Ruby yet to actually make any judgement. It is just a bit of cotton candy for the brain right now.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #22 on: March 20, 2009, 10:38:22 PM »
I prefer Python over Ruby for the simple matter that it aligns with my thought processes better than Ruby and I can concentrate my efforts more on solving the problem rather than the semantics of the language. Ruby had some appeal because of the Rails MVC framework, but the syntax, as concise and as expressive as it may be annoys me to be honest.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

SomeCallMeDave

  • Guest
Re: ( Challenge ) StringToBinary
« Reply #23 on: March 20, 2009, 11:19:27 PM »
I prefer Python over Ruby for the simple matter that it aligns with my thought processes better than Ruby and I can concentrate my efforts more on solving the problem rather than the semantics of the language. Ruby had some appeal because of the Rails MVC framework, but the syntax, as concise and as expressive as it may be annoys me to be honest.

A book on Rails in the local library is where I first was introduced to Ruby.  I don't have any need for Rails (wish that I did) but I sort of latched onto Ruby as my current time-waster-of-choice.   

I do need to give Python another look.   There seems to be plenty of interest in the Swamp now so maybe I can pick up a bit.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: ( Challenge ) StringToBinary
« Reply #24 on: March 21, 2009, 02:06:09 AM »
While I slept, you have already finished Challenge?  :-o

Code: [Select]
(defun eea-dec-bin8 (a)
 (apply 'strcat (mapcar '(lambda (b) (itoa (rem (lsh a (- b)) 2))) '(7 6 5 4 3 2 1 0)))
)

Code: [Select]
(mapcar 'eea-dec-bin8 (vl-string->list "ABC"))

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: ( Challenge ) StringToBinary
« Reply #25 on: March 21, 2009, 04:12:21 AM »
nice move, Evgeniy! :)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: ( Challenge ) StringToBinary
« Reply #26 on: March 21, 2009, 07:41:38 AM »
Nice Evgeniy,

You give me some inspiration to try a quite shorter C# code

Code: [Select]
static string[] StringToBinary(string str)
{
    Byte[] bytes = Encoding.ASCII.GetBytes(str);
    string[] result = new string[bytes.Length];
    for (int i = 0; i < bytes.Length; i++)
    {
        string bin = "";
        for (int e = 7; e >= 0; e--)
             bin += ((bytes[i] >> e) % 2).ToString();
        result[i] = bin;
    }
    return result;
}
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: ( Challenge ) StringToBinary
« Reply #27 on: March 21, 2009, 07:47:00 AM »
> gile, VovKa
Thanks!  :-)


MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #28 on: March 21, 2009, 09:41:38 AM »
While I slept, you have already finished Challenge?  :-o

Very nicely done sir!

You give me some inspiration to try a quite shorter C# code

Nice translation Gile! However, you're returning a string, in C# you should return a string array. :)

Translating Gile's line by line to Python but returning a list:

Code: [Select]
def stringToBinary(s):
    bytes=list(map(ord,s)); result=[]
    for i in range(0,len(bytes)):
        bin=""
        for e in range(7,-1,-1):
            bin+=str((bytes[i] >> e) % 2)
        result+=[bin]
    return result

Wonder how it performs compared to:

Code: [Select]
def stringToBinary(s):
    return list(map(lambda a: (('0'*8)+bin(ord(a))[2:])[-8:],s))

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #29 on: March 21, 2009, 10:10:56 AM »
I do need to give Python another look. There seems to be plenty of interest in the Swamp now so maybe I can pick up a bit.

If for no other reason than peer support you may find that Python wins, at least in the CADD community where it seems to be developing more momentum. An aside, I've found there's far more books available on Python than Ruby, a big selling point for me, an admitted computer book junkie. Collyer bros anyone? lol
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst