Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def read_a_list(list):
- while True:
- try:
- real_part=input()
- imag_part=input()
- if real_part == "exit" or imag_part == "exit":
- break
- real_part = int(real_part)
- imag_part = int(imag_part)
- list.append((real_part,imag_part))
- except ValueError as i:
- print("invalid input!")
- return list
- def print_complex(a): #in this way we print a complex number
- print(str(a[0]) + " + " + str(a[1]) + "i")
- def print_tuple(list):
- for a in list:
- print_complex(a)
- def real(a): #this function returns the real part of a complex number from a tuple
- return a[0]
- def imag(a): #this function returns the imaginary part of a complex number from a tuple
- return a[1]
- def increasing_real_part(list):#this function returns the longest sequence of numbers with strictly increasing real part
- max_nr_elements=0
- nr_elements=1
- sequence=[]
- sequence.append(list[0])
- for i in range(len(list)-1):
- if real(list[i])>real(list[i+1]):
- nr_elements+=1
- sequence.append(list[i])
- else:
- if nr_elements>max_nr_elements:
- max_nr_elements=nr_elements
- max_sequence=sequence
- sequence=[]
- nr_elements=0
- if nr_elements>max_nr_elements:
- max_sequence=sequence
- return max_sequence
- def modul(c):
- return (c[0]*c[0]+c[1]*c[1])**(1/2.0)
- def same_modules(l):
- max_nr_elements=0 #the number of elements of the maximum sequence
- begin_index =0
- max_begin_index = 0
- #sequence.append(l[0]) #we save the first complex number to the current maximum sequence
- nr_elements=0 #the number of elements of the current sequence
- for i in range(len(l)-1):
- print(max_begin_index,nr_elements)
- if modul(l[i])==modul(l[i+1]):
- nr_elements+=1
- else:
- if nr_elements > max_nr_elements:
- max_nr_elements=nr_elements
- max_begin_index = begin_index
- begin_index= i
- nr_elements=0
- return l[max_begin_index:max_begin_index+max_nr_elements]
- def print_sequence(sequence):
- print_tuple(sequence)
- def print_same_modules(list):
- max_sequence=same_modules(list)
- print_sequence(max_sequence)
- def print_increasing_real_part(list):
- max_sequence=increasing_real_part(list)
- print_sequence(max_sequence)
- def run():
- list=[(1,2),(1,3),(3,1),(0,2),(7,5),(9,10),(10,9),(14,90),(1,12),(7,9)]
- commands={"1":read_a_list, "2":print_tuple, "3":print_same_modules, "4":print_increasing_real_part}
- while True:
- command=input("Give a command: 1.Read a list. 2.Print the list. 3.Print the longest sequence which contains numbers with the same modules. 4. Print the longest sequence which contains numbers with a strictly increasing part.")
- if command=="exit":
- return
- if command in commands:
- commands[command](list)
- else:
- print("Illegal command!")
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement