"""
Full name: Stefan Guettel
StudentId: 123456
Lab class: AB
Email: stefan.guettel@manchester.ac.uk
"""
def rev(n):
"""
Returns the number that is obtained from the integer `n` by
reversing the order of the digits.
Copied from lab-03-04.py.
"""
res = 0
if n < 0:
n = -n
sign = -1
else:
sign = 1
while n > 0:
res = 10*res + n%10
n //= 10
return sign*res
def rearrange(L):
"""
This function accepts a list of integers `L` and returns a
new list of integers with the elements rearranged in reverse
order, and with every element having its digits reversed.
"""
newL = list()
for el in L:
newL.insert(0,rev(el))
return newL
def oddeven(L, a, b):
"""
`oddeven(L, a, b)` excepts a nonempty list of integers `L`
and two integers `0 <= a <= b < len(L)`. It returns a new
list of integers containing all the odd numbers among the
elements of L with even indices between a and b (including
both of them) in their original order.
"""
newL = list()
for i in range(a, b+1):
if i%2==0 and L[i]%2==1:
newL.append(L[i])
return newL
#-----------------------------------------------------------------
def evenodd(L):
"""
`evenodd(L)` excepts a list of integers `L`. It returns a new
list of integers with all even numbers in `L` appear first
in reversed order, followed by all odd numbers in `L` in
their original order.
"""
newL = list()
for i in range(0, len(L)):
if L[i]%2==0:
newL.insert(0,L[i])
else:
newL.append(L[i])
return newL
def prime(n):
"""
Returns `True` if `n` is a prime number and `False` otherwise.
Copied from exercise-03-03.py.
"""
if n < 2:
return False
for k in range(2, n):
if n % k == 0:
return False
return True
def prime_slice(L, a, b):
"""
`oddeven(L, a, b)` excepts a nonempty list of integers `L`
and two integers `0 <= a <= b < len(L)`. It returns a new
list of integers containing all the odd numbers among the
elements of L with even indices between a and b (including
both of them) in their original order.
"""
newL = list()
for i in range(a, b+1):
if prime(L[i]):
newL.append(L[i])
return newL
def main():
# TODO: Test your functions here
print(rearrange([]))
print(oddeven([11, 12, 3, 17, 20, 22, 2, 19, 7, 42, 19, 20], 1, 10))
print(evenodd([5, 62, 9, 84, 80, 7]))
print(prime_slice([11, 12, 3, 17, 20, 22, 3, 19, 7, 42, 19, 20], 2, 10))
main()