-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoldbach Deuce
132 lines (88 loc) · 3.66 KB
/
Goldbach Deuce
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
## Name: Jaewon Son
## Date: October 01 2023
## Honor Statement: I have not given or received any unauthorized assistance on this assignment.
## Link: https://youtu.be/sHEvagz2yrI
import random
def ask_length():
"""Function to ask length input from a user to create a list.
Returns:
int: Return a integer between 0 and 100.
"""
while True:
input_length = input("\nPlease enter any random integer between 0 and 100. Or you can type 'exit' to terminate the program: ")
if input_length == 'exit':
return 'exit'
try:
input_length = eval(input_length)
assert type(input_length) == int # Checking whether the input is an integer
assert input_length >= 0 # Preventing the input from going out of the range of 0 to 100
assert input_length <= 100
return int(input_length)
except:
print("\nInvalid input. Please enter between 0 and 100. Or you can type 'exit' to terminate the program.")
def generate_list(input_length):
"""Function to create a random numbers list based on the input.
Returns:
list: Return list of a random numbers.
"""
list = random.sample(range(1, 101), input_length) # Creating a list of random numbers
list.sort() # Sorting the list for binary search
return list
def ask_sum():
"""Function to ask a target sum input from a user.
Returns:
int: Return a positive integer.
"""
while True:
input_sum = input("\nPlease enter any positive integer which is to be a sum: ")
try:
input_sum = eval(input_sum)
assert type(input_sum) == int # Checking whether the input is an integer
assert input_sum >= 1 # Preventing the input from going out of the range of 1 to 199
assert input_sum <= 199
return int(input_sum)
except:
print("\nInvalid input. Please enter a positive integer.")
def binary_search(sum, list):
"""Binary serach to find a value in a list.
Returns:
bool: Returns true when find a proper value, else returns false.
"""
value1 = 0
value2 = len(list) - 1
while value1 <= value2:
mid = int((value1 + value2) / 2) # Index of middle
mid_value = list[mid]
if sum == mid_value:
return mid
elif sum < mid_value: # Sum in lower half
value2 = mid - 1
else:
value1 = mid + 1 # Sum in upper half
return False
def find_two_pair(binary_search, sum, list):
"""Function to generate an output.
"""
for number1 in list:
number2 = sum - number1
if binary_search(number2, list) and number1 != number2: # Checking two numbers are in the generated list
return(f"\n{number1} and {number2} in the list are summed to {sum}.\n")
return(f"\nNone of the two numbers in the list sum to {sum}.\n")
def exit_program():
"""Function to terminate the program.
"""
print("\nThank you for using the program!\n")
def main_function():
"""Main function for the Goldbach deuce.
"""
while True:
input_length = ask_length()
if input_length != 'exit':
list = generate_list(input_length)
input_sum = ask_sum()
print(f"\nThis is the list generated through a random integer: {list}")
print(find_two_pair(binary_search, input_sum, list))
else:
exit_program()
break
main_function()