problem with some functions

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

Join Date: Oct 2009
Posts: 18
Reputation: saikeraku is an unknown quantity at this point 
Solved Threads: 0
saikeraku saikeraku is offline Offline
Newbie Poster

problem with some functions

 
0
  #1
Nov 2nd, 2009
Hi

I'm trying to write some functions.

This first function is suppose to help shuffle two strings which are the same length into the correct word.
So the output would be something like:

After shuffling the first string som and the second string trs the word is storms.

Here is my code now:
I don't know if I'm starting this correctly, hopefully I am though. Problem with this is that I don't know how to rearrange the a[] and b[] to reorder it in order from [0] onwards.
  1. def reorder(a, b):
  2. """
  3. returns a "shuffled" version of a + b:
  4. something like a[0] + b[0] + a[1] + b[1] + a[2] + b[2] + so on...
  5. """
  6.  
  7. reorder_str = a + b
  8. for i in range(len(reorder_str)):
  9. reorder_str = a[i] + b[i]
  10. return reorder_str
  11.  
  12.  
  13. #Testing function:
  14.  
  15. first_str = "som"
  16. second_str = "trs"
  17.  
  18. final = reorder(a, b)
  19.  
  20. print "After shuffling the first string " +str(first_str) + " and the second string" + " " + str(second_str) + " the word is" + " " + str(reorder) + "."


The second function I'm trying to create is to find even numbers within a list and return a new list that only gives the elements divisible by 2.
The original list itself should not be changed though.
So for example:

lista = [1,2,5,6,7]
evenlist = even_list(lista)

print lista
print evenlist

[1,2,5,6,7]
[2, 6]

My code right now is:
(The problem I have here is that I don't know how to output the new list with only even numbers)
  1. def even_list(a):
  2. """
  3. original list will remain unchanged and the new list will only
  4. return the integers that are even
  5. """
  6.  
  7. lista = []
  8. for i in range(len(lista)):
  9. if i % 2 == 0:
  10. lista = lista % 2
  11.  
  12. return lista
  13.  
  14.  
  15. #Testing the function
  16.  
  17. lista = [1,2,5,6,7]
  18. evenlist = even_list(lista)
  19.  
  20. print lista
  21. print evenlist


Thanks for any help/hints/explanation/suggestions.
Last edited by saikeraku; Nov 2nd, 2009 at 4:42 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,151
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 952
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #2
Nov 2nd, 2009
In your second problem in function even_list the for loop should iterate through the list you are giving the function. If you find an item in that list that is even, then you have to append it to the list you call lista.

If you are trouble shooting, it always helps to temporarily put in a test print to show an interim result.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster
 
1
  #3
Nov 2nd, 2009
For the first problem if you require to make a new meaningful word out of a set of characters, then you need to have a dictionary with a number of words. Pass in the two strings as an array of characters. Iterate through your dictionary of words for the first matching meaningful word with all the characters, send it back to the calling function.

If you are looking for a[0] + b[0] + a[1] + b[1] + a[2] + b[2] then you take [i] of string a and append it to your new string, then similarly take [i] of string b and append it to your new string. At the end of the, append all the remaining characters.

In your second problem all you need to do is take each element from list and check whether it's even or not, if it is even append it to the new list. return the new list.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 52
Reputation: d5e5 is an unknown quantity at this point 
Solved Threads: 7
d5e5's Avatar
d5e5 d5e5 is offline Offline
Junior Poster in Training
 
1
  #4
Nov 2nd, 2009
final = reorder(a, b)
Look at line 18 of your first example, where you test your first function. Variables a and b have meaning only in your function. You need to pass the variables first_str and second_str when you call your function, not a and b, which are out of scope until control is passed to your function.

for i in range(len(reorder_str)):
Also look at the for loop in your first function. It loops through a range of 0 up to the length of reorder_str. The strings in variables a and b each have length of 3, so what happens when variable i = 4? a[4] and b[4] are undefined.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: saikeraku is an unknown quantity at this point 
Solved Threads: 0
saikeraku saikeraku is offline Offline
Newbie Poster
 
0
  #5
Nov 4th, 2009
For the second one I tried something like this:


Nvm, I got the second one working, going to try the first one now.

Thanks for all the suggestions!
Last edited by saikeraku; Nov 4th, 2009 at 2:15 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training
 
0
  #6
Nov 4th, 2009
Below are some mistakes you are doing,
1. calling reorder() without any args, in print fuction.
2. you cannot refer a char at a[a+b] which you are doing it in for loop in the reorder fuction, because of IndexError. Instead you need to loop through the size of either of string, as you already stated in the problem that two strings are of same length.
3. you are assigning new string value to reorder_str everytime you loop.

Below is just corrected form of for loop in reorder fuction.
  1. reorder_str = ""
  2. for i in range(len(a)):
  3. reorder_str += a[i] + b[i]
  4. return reorder_str

katharnakh.
Last edited by katharnakh; Nov 4th, 2009 at 3:16 am.
Reply With Quote Quick reply to this message  
Reply

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




Views: 325 | Replies: 5
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC