Repository navigation
Expand file tree
/
Copy pathselect_which.py
More file actions
81 lines (76 loc) · 2.59 KB
/
Copy pathselect_which.py
File metadata and controls
81 lines (76 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def replace_in_string(string):
return string.replace(' ', '-')
def char_last_in_string(string, char):
for i in range(len(string)-1, -1, -1):
if string[i] == char:
return i
def prime_factors(num):
factors = []
for i in range(2, num + 1):
if num % i == 0:
prime = True
for j in range(2, i // 2 + 1):
if i % j == 0:
prime = False
break
if prime:
factors.append(i)
return factors
def swap_num(x, y):
return y, x
def separate_odd_even(num_list):
odd = []
even = []
for num in num_list:
if num % 2 == 0:
even.append(num)
else:
odd.append(num)
return even, odd
if __name__=="__main__":
import sys
select = input("""\
Please select which function you wish to execute....
Hint: Provide an integer value corresponding to the function in the order given below.
>>>
>>> 1 Replace empty space with hyphen in a string
>>> 2 Find last occurrence of a character in a string
>>> 3 Find prime factors of a number
>>> 4 Swap two numbers
>>> 5 put even and odd numbers in separate list
>>>
.......: """)
if select == '1':
string = input("""\
Please give your string to act upon:
.......: """)
print(replace_in_string(string))
elif select == '2':
string = input("""\
Please give your string to act upon
.......: """)
char = input("""\
Please give the charater to find its last occurrence
.......: """)
print(f"At the last occurrence, the index of {char} is {char_last_in_string(string, char)}")
elif select == '3':
num = int(input("""\
Please give a number to find its prime factors
.......: """))
print(f"Prime factors of {num} are: {prime_factors(num)}")
elif select == '4':
inp = input("""\
Please give two comma (,) separated numbers to swap
.......: """)
ls = inp.split(',')
x, y = int(ls[0]), int(ls[1])
print(f"Swapped Numbers: {swap_num(x, y)}")
elif select == '5':
inp = input("""\
Please give comma (,) separated numbers to put in to even and odd lists
.......: """)
num_list = [int(i) for i in inp.split(',')]
even, odd = separate_odd_even(num_list)
print(f"List of evens: {even}, and odds: {odd}")
else:
print("Incorrect Input! You are suppose to give a integer number between 1 and 5 (inclusive)")