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.

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

[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)

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.

Edited 7 Years Ago by saikeraku: n/a

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.

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 of string a and append it to your new string, then similarly take 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.

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.

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!

Edited 7 Years Ago by saikeraku: n/a

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.

reorder_str = ""
for i in range(len(a)):
    reorder_str += a[i] + b[i]
return reorder_str


Edited 7 Years Ago by katharnakh: n/a

This question has already been answered. Start a new discussion instead.