| | |
problem with some functions
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
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.
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)
Thanks for any help/hints/explanation/suggestions.
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.
Python Syntax (Toggle Plain Text)
def reorder(a, b): """ returns a "shuffled" version of a + b: something like a[0] + b[0] + a[1] + b[1] + a[2] + b[2] + so on... """ reorder_str = a + b for i in range(len(reorder_str)): reorder_str = a[i] + b[i] return reorder_str #Testing function: first_str = "som" second_str = "trs" final = reorder(a, b) 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)
Python Syntax (Toggle Plain Text)
def even_list(a): """ original list will remain unchanged and the new list will only return the integers that are even """ lista = [] for i in range(len(lista)): if i % 2 == 0: lista = lista % 2 return lista #Testing the function lista = [1,2,5,6,7] evenlist = even_list(lista) print lista print evenlist
Thanks for any help/hints/explanation/suggestions.
Last edited by saikeraku; 34 Days Ago at 4:42 am.
0
#2 34 Days Ago
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.
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!
1
#3 34 Days Ago
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
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.
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.
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 6
1
#4 34 Days Ago
•
•
•
•
final = reorder(a, b)
•
•
•
•
for i in range(len(reorder_str)):
0
#6 32 Days Ago
Below are some mistakes you are doing,
1. calling
2. you cannot refer a char at
3. you are assigning new string value to
Below is just corrected form of for loop in reorder fuction.
katharnakh.
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.
python Syntax (Toggle Plain Text)
reorder_str = "" for i in range(len(a)): reorder_str += a[i] + b[i] return reorder_str
katharnakh.
Last edited by katharnakh; 32 Days Ago at 3:16 am.
![]() |
Similar Threads
- Major problem with functions. Need help! (C)
- tkinter widget method problem (Python)
- standard io problem (C++)
- Need help writing a wrapper function for fork() and signal functions. (C)
- Programing functions and having a problem with one of the functions. (C++)
- Array Index Problem in Quick Sort (C#)
- Functions with a structure output. (C)
- C - Functions and arrays (C)
Other Threads in the Python Forum
- Previous Thread: update namespace
- Next Thread: I don't know what is causing this error...please help
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment backend beginner binary book builtin calculator character chmod converter countpasswordentry curved customdialog dan08 dictionaries dictionary drive dynamic examples exe file filename float format function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pysimplewizard python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame write wxpython






