Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a "formal" program that takes an even number list and prints and returns a list in which every even number from the input list has been multiplied by the odd number behind it and every odd number has been divided by the even number after it.

eg [1,2,3,4] -> [0.5, 2, 0.75, 12]

i have this but i don't know if it is good

Python
def numprogram(l):
    count = 0
    for i in l:
        count +=1
        if i % 2 == 0:
            l[i] = i * (i-1)
            l += l[i]
        else:
            l[i] = i / (i+1)
            l += l[i]
Posted
Updated 5-Dec-14 18:29pm
v6
Comments
PIEBALDconsult 5-Dec-14 23:41pm    
Seems quite simple. What have you tried? Please use Improve question to show what you have so far and where your are stuck.
Sammyw10 5-Dec-14 23:46pm    
so i need the output to be a list, but when i ran this with numprogram([1,2,3,4]) , it returned "0"
PIEBALDconsult 5-Dec-14 23:50pm    
You don't want to return, you want to output or print or whatever it is in Python.
I think you'll want another variable, you should probably not be altering the value of i.
The elif could simply be an else, no need to test again.
Sammyw10 5-Dec-14 23:51pm    
I just tried that, And it gives me 0
PIEBALDconsult 5-Dec-14 23:56pm    
Update the question with the code as it is now.

1 solution

I have done some changes in your code. Result as you required. [0.5, 2, 0.75, 12].

def numprogram(l):
    temp = list()
    count = 0
    for i in l:
        count +=1
        if i % 2 == 0:
            l =  i * (i-1)
            temp.append(l)
        else:
            l = i/float((i+1))
            temp.append(l)
    return temp
            
print numprogram([1,2,3,4])
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900