Python challenge help

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2008
Posts: 35
Reputation: Darkangelchick is an unknown quantity at this point 
Solved Threads: 1
Darkangelchick Darkangelchick is offline Offline
Light Poster

Python challenge help

 
0
  #1
Aug 7th, 2008
Hey guys im doing the NCSS python challenge and I need help!!
ok so here's my current problems (p.s. my teach cant even help)
PROBLEM 1
************************
  1. # This is a Python file
  2. def fibonacci(n):
  3. return (n <= 1) and 1 or fibonacci(n-1) + fibonacci(n-2)
  4.  
  5. def decrypt(s):
  6. def helper(s):
  7. return s[::-1]
  8. return helper(s)

I need code 2 ouput this .....
fibonacci
decrypt
helper

so pretty much i need help on callin function names ( I think)



PROBLEM 2
****************

UMTPORMHTRNSDCYTDTIEPREITIPATAGRAIBS

Hint

Yes, that is the whole question.
A very simple technique has been used to encrypt the message. When you decrypt it, you will know how to solve the problem.
It is not a substitution cipher. It's even simpler than that.


UHHHHHH HELP!!!


PROBLEM 3
***********************
Surprisingly, the SETI project has found alien life! They have been transmitting floating-point numbers to earth using a base-5 number system. The five characters for the digits in their system are the five English vowels, ordered alphabetically, so a=0, e=1, i=2, o=3, and u=4 (in decimal).

The aliens use a clever scheme without a separator between the integer and fractional part of the float. They use capital vowels for the integer part, followed by lowercase vowels for the fractional part.

For instance, IUae has an integer part IU and a fractional part ae. The corresponding floating point value in decimal is I*5 + U*1 + A/5 + E/25 = 2*5 + 4 + 0/5 + 1/25 = 14.04. Either the integer or fractional part may appear without the other.

Write a Python function called alien2float that converts a string in the alien number format into a Python float. When we call this function like this, this is what happens:

>>> alien2float("IUae")
14.039999999999999
Your function should return -1 when the string does not conform to the alien notation defined above.

>>> alien2float("iuAE")
-1
Last edited by Tekmaven; Aug 8th, 2008 at 4:42 pm. Reason: Code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Python challenge help

 
0
  #2
Aug 8th, 2008
Please use code tags when posting blocks of code, and icode tags when posting small snippets. This will make sure your formatting is not lost to the ether; and will also make your posts easier for us to read; thereby making it easier for us to help you. You can read up on code tags here
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 972
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Python challenge help

 
0
  #3
Aug 8th, 2008
for PROBLEM 1, I suggest the following program
  1. #!/usr/bin/env python
  2.  
  3. # printdef.py
  4. # prints the names of all function definitions found in a program
  5. # usage: python printdef.py myprogram.py
  6.  
  7. from tokenize import generate_tokens, NAME
  8.  
  9. def getDefSequence(programPath):
  10. tokens = generate_tokens(open(programPath, "r").readline)
  11. for token in tokens:
  12. if token[0] == NAME and token[1] == "def":
  13. func = tokens.next()
  14. if func[0] == NAME:
  15. yield func[1]
  16.  
  17.  
  18. if __name__ == "__main__":
  19. import sys
  20. programPath = sys.argv[-1]
  21. if programPath[-3:] != ".py":
  22. print "usage: printdefs.py <path to '.py' file>"
  23. sys.exit(-1)
  24. for name in getDefSequence(programPath):
  25. print name
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 972
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Python challenge help

 
0
  #4
Aug 8th, 2008
if you like the previous code, I'll put it in the code snippets
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 6
Reputation: griefers is an unknown quantity at this point 
Solved Threads: 3
griefers griefers is offline Offline
Newbie Poster

Re: Python challenge help

 
0
  #5
Aug 8th, 2008
For question 2:
decrypted by using:
  1. str = "UMTPORMHTRNSDCYTDTIEPREITIPATAGRAIBS"
  2. count = 0
  3. length = len(str)/2
  4. while count<length:
  5. print str[len(str)-1-count],
  6. print str[count],
  7. count+=1
which give:
S U B M I T A P R O G R A M T H A T P R I N T S I D E C R Y P T E D I T
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 972
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Python challenge help

 
0
  #6
Aug 8th, 2008
I give my solution for PROBLEM 3 in obfuscated python
  1. def alien2float(s):
  2. from bisect import bisect
  3. i = bisect(s, '[')
  4. seti = lambda z: reduce(lambda x, y: 5.0*x+y,
  5. map(dict(map(lambda(x, y): (y, x), enumerate("AEIOU"))).get, z))
  6. try:
  7. if i:
  8. res = seti(s[:i])
  9. else:
  10. res = 0.0
  11. x = s[i:]
  12. if not x:
  13. return res
  14. elif not x.islower():
  15. return -1
  16. return res + seti(x.upper())/5**len(x)
  17. except TypeError:
  18. return -1
Last edited by Gribouillis; Aug 8th, 2008 at 2:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 10
Reputation: friendklay is an unknown quantity at this point 
Solved Threads: 2
friendklay friendklay is offline Offline
Newbie Poster

Re: Python challenge help

 
0
  #7
Aug 8th, 2008
This is for problem 3.
  1. import re
  2.  
  3. def alien2float(n):
  4. match = re.match("([aeiou])([AEIOU])", n)
  5. if match:
  6. print "-1"
  7. exit()
  8. result = re.sub("([AEIOU])([aeiou])", "\\1.\\2", n)
  9. result = re.sub("A", "0", result)
  10. result = re.sub("E", "1", result)
  11. result = re.sub("I", "2", result)
  12. result = re.sub("O", "3", result)
  13. result = re.sub("U", "4", result)
  14.  
  15. result = re.sub("a", "0", result)
  16. result = re.sub("e", "1", result)
  17. result = re.sub("i", "2", result)
  18. result = re.sub("o", "3", result)
  19. result = re.sub("u", "4", result)
  20.  
  21. result = re.split("\.", result)
  22.  
  23. lefthand = int(result[0], 5)
  24. righthand = list(result[1])
  25.  
  26. decimal = 0
  27. count = 5
  28. for a in righthand:
  29. decimal = decimal + (float(a) / (count))
  30. count = count * 5
  31. result = lefthand + decimal
  32. print result
  33.  
  34. if __name__ == "__main__":
  35. alien2float("IUae")

The regular expression to catch invalid data needs to be modified. For valid data, the above code should work. It also doesn't check for "Either the integer or fractional part may appear without the other." Both must be present to work.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 948
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 146
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Python challenge help

 
0
  #8
Aug 8th, 2008
just quickly i wanted to remind Darkangel or the NCSS Challenge rules. Rule 3 stated that:

"The solutions you submit must be your own. It is fine to discuss the problems, and to read code in books or on web sites to get ideas, but you must be the author of any code you submit. It is not okay to copy anybody else's code and submit that as if it is your own."

This just means discuss it all you like but you are not allowed to directly use code that other people have made.
People at Daniweb just remember for this comp that people are not meant to do get direct answers.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 35
Reputation: Darkangelchick is an unknown quantity at this point 
Solved Threads: 1
Darkangelchick Darkangelchick is offline Offline
Light Poster

Re: Python challenge help

 
0
  #9
Nov 26th, 2008
Originally Posted by Gribouillis View Post
if you like the previous code, I'll put it in the code snippets
Yeah that was awesome, i had 2 adjust it for the comp but it helped THANX!!! lol noob i am ha ha
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 35
Reputation: Darkangelchick is an unknown quantity at this point 
Solved Threads: 1
Darkangelchick Darkangelchick is offline Offline
Light Poster

Re: Python challenge help

 
0
  #10
Nov 26th, 2008
Originally Posted by paulthom12345 View Post
just quickly i wanted to remind Darkangel or the NCSS Challenge rules. Rule 3 stated that:

"The solutions you submit must be your own. It is fine to discuss the problems, and to read code in books or on web sites to get ideas, but you must be the author of any code you submit. It is not okay to copy anybody else's code and submit that as if it is your own."

This just means discuss it all you like but you are not allowed to directly use code that other people have made.
People at Daniweb just remember for this comp that people are not meant to do get direct answers.

Thanx for the reminder but i just needed a lil help to get the jist of the solution. The questions were even alien to my teacher and he couldnt help so i had 2 ask somewhere
Im still learning and im a first year coder so be nice
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC