User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 402,793 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,860 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 335 | Replies: 7
Reply
Join Date: Aug 2008
Posts: 1
Reputation: Darkangelchick is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Darkangelchick Darkangelchick is offline Offline
Newbie Poster

Python challenge help

  #1  
30 Days Ago
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
************************
# This is a Python file
def fibonacci(n):
  return (n <= 1) and 1 or fibonacci(n-1) + fibonacci(n-2)

def decrypt(s):
  def helper(s):
    return s[::-1]
  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 : 29 Days Ago at 3:42 pm. Reason: Code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 139
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 21
jlm699's Avatar
jlm699 jlm699 is offline Offline
Junior Poster

Re: Python challenge help

  #2  
30 Days Ago
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
Let's Go Pens!
Reply With Quote  
Join Date: Jul 2008
Posts: 121
Reputation: Gribouillis is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 16
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Junior Poster

Re: Python challenge help

  #3  
30 Days Ago
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  
Join Date: Jul 2008
Posts: 121
Reputation: Gribouillis is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 16
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Junior Poster

Re: Python challenge help

  #4  
30 Days Ago
if you like the previous code, I'll put it in the code snippets
Reply With Quote  
Join Date: Jun 2008
Posts: 6
Reputation: griefers is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
griefers griefers is offline Offline
Newbie Poster

Re: Python challenge help

  #5  
30 Days Ago
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  
Join Date: Jul 2008
Posts: 121
Reputation: Gribouillis is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 16
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Junior Poster

Re: Python challenge help

  #6  
29 Days Ago
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
  19.  
Last edited by Gribouillis : 29 Days Ago at 1:57 pm.
Reply With Quote  
Join Date: Aug 2008
Posts: 8
Reputation: friendklay is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
friendklay friendklay is offline Offline
Newbie Poster

Re: Python challenge help

  #7  
29 Days Ago
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  
Join Date: May 2008
Location: Australia
Posts: 95
Reputation: paulthom12345 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
paulthom12345's Avatar
paulthom12345 paulthom12345 is offline Offline
Junior Poster in Training

Re: Python challenge help

  #8  
29 Days Ago
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 Blog - paulthom12345.blogspot.com
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 10:29 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC