python code...convert to ksh or sh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting python code...convert to ksh or sh
# 1  
Old 01-22-2007
python code...convert to ksh or sh

Helloo...

I am not much familiar with python..I found this small script in python I even do not have python on my computer...can anyone help me out to convert this into ksh or sh..

PLEASE any help I will appreciate..

here is python code..

#!/usr/bin/env python
import random # Get a random number generator.
NTRIALS = 10000 # Enough trials to get an reasonably accurate answer.
NPEOPLE = 30 # How many people in the group?
matches = 0 # Keep track of how many trials have matching birthdays.
for trial in range(NTRIALS): # Do a bunch of trials...
taken = {} # A place to keep track of which birthdays
# are already taken on this trial.
for person in range(NPEOPLE): # Put the peoples birthdays down, one at a time...
day = random.randint(0, 365) # On a randomly chosen day.
if day in taken:
matches += 1 # A match!
break # No need to look for more than one.
taken[day] = 1 # Mark the day as taken.




I wanted to see how does it work...I have no experience in python that is why I ask if anyone can help me out to compile it thru ksh or sh...

I would not like to install python..I would prefare if someone can help me out with his..becouse I plan to upgrade this method..and add new modules in shell..??

Regards..any help is very welcome????
# 2  
Old 01-22-2007
i think this is all u need to convert this script ! hf ! Smilie
http://www.cyberciti.biz/nixcraft/li...features/lsst/
# 3  
Old 01-22-2007
This Python script is very clear in what its supposed to do.

1) import random
This is a library to generate random numbers
In bash , you can use the RANDOM env variable
2)
Code:
NTRIALS = 10000
matches = 0

For these, they are the same as declaring shell variables
3)
Code:
for trial in range(NTRIALS): # Do a bunch of trials...
    taken = {} 
    for person in range(NPEOPLE): 
        day = random.randint(0, 365) 
        if day in taken:
            matches += 1 
            break # No need to look for more than one.
        taken[day] = 1

range(NTRIALS) will give you a range of values from 0 to 9999
so for trial in range(NTRIALs) is a for loop to generate these numbers. So this for loop will iterate 10000 times, starting from count 0.
matches+=1 is the same as matches = matches + 1, which in shell, you can use something like this (or other methods)
Code:
x=`echo "$x + 1" | bc`

taken = {} declares a dictionary, or a lookup table.
random.randint(0,365) will generate a random number in the range from 0 to 365. taken[day] = 1 means keep that random day number in the dictionary
with the value of 1.
taken will now contain for example:
Code:
taken = { 102:1 } # 102 generated from random.randint()

# 4  
Old 01-22-2007
thanks for your reply..I will try to figure it in KSH..I hope there will be someone to help me with more details..

thanks..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to convert dec to hex in python?

When I try to convert big numbers I get extra numbers at the end that doesn't move plus an L character too. How to remove the 4 extra characters at the end 000L? 8b8dbbc584d9c000L 8b8dc4ddd34c6000L 8b8dcdf621bf0000L 8b8dd70e7031a000L 8b8de026bea44000L #!/usr/bin/python ... (9 Replies)
Discussion started by: bigvito19
9 Replies

2. Shell Programming and Scripting

Can someone convert this python script to perl

There is a python script that I would like converted to a perl script. If someone has the time to convert the script I would appreciate it. You can find the script below: reboot-mb8600/reboot-mb8600.py at master . j4m3z0r/reboot-mb8600 . GitHub #!/usr/bin/python ''' A hacky script to... (1 Reply)
Discussion started by: azdps
1 Replies

3. Shell Programming and Scripting

Convert excel to csv in python date not display exactly

Hi, Anyone can help I am just converting the excel file to csv using python, I can get the conversion output but the date not display exactly. test.xlsx date format 167 1588 risks/SCS JP CAMPANA & CIE.pdf SCS JP CAMPANA & CIE 2 1 1 0 2015-03-16 16:56:25 167 1146 risks/AirBNB... (1 Reply)
Discussion started by: fspalero
1 Replies

4. Shell Programming and Scripting

Convert int to string in python

Hi, I have column 5 in a file which contains string like this for ex. RP11-125O5.2 SLCO1B1 CAPN1 FRMPD2 TXNL4B So I do by data = )] ValueError: invalid literal for int() with base 10: 'R' Can someday tell me how to convert this column into int successfully. Thank You in... (7 Replies)
Discussion started by: rossi
7 Replies

5. Shell Programming and Scripting

Python script to convert date to iso format

Hi , This is my 1st program in python never tried any python before. i am trying to write a python script which reads a .tsv file line by line and in each line it should look for mm/dd/yyyy formate and convert it to yyyy-mm-dd formate . can some one provide be some sample code to do that. (2 Replies)
Discussion started by: vikatakavi
2 Replies

6. Shell Programming and Scripting

Convert Hex - KSH

Hello, I woild like to convert hex on KSH not BASH: I tried to use: tmp=31 printf "\x"${tmp}"" it works on bash - Output is '1' but not on ksh. please advice on the right syntax. Thanks. (4 Replies)
Discussion started by: LiorAmitai
4 Replies

7. UNIX for Dummies Questions & Answers

convert a file from .ksh to .sh

hi I have written a shell scrpit in .ksh mode. I need to convert this scritp to .sh. What things i need to check while converting the file from .ksh to .sh. Could you please help me out. Thanks&Reagdrs Pavan Kumar (3 Replies)
Discussion started by: vnspkumar
3 Replies

8. Shell Programming and Scripting

convert ksh to C program

Hi Guys...is there a way to convert a .ksh script to .C program..? (3 Replies)
Discussion started by: aggars
3 Replies

9. Shell Programming and Scripting

Need to convert ksh script to Perl

Guys I am new to this forum, this may seem like a cheeky request. I have been asked by my manager to convert this ksh script to Perl. I do not have the foggiest about Perl and would appreciate any help on this. Basically this scipt automates a recovery process for EMC Legato Networker. It will... (1 Reply)
Discussion started by: rahimm1
1 Replies

10. UNIX for Dummies Questions & Answers

convert from ksh to bsh

How difficult is it to convert from bourn shell to korn shell and visversa. Reason: If someone bombsout or logs out incorrectly the application we are using give the next person to log in the same permissions as the person that bombed out. Therefore createing ownership problems on print files. (2 Replies)
Discussion started by: Boyd Young
2 Replies
Login or Register to Ask a Question