Advertisement
iulianapascotescu

Untitled

Oct 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. def read_a_list(list):
  2. while True:
  3. try:
  4. real_part=input()
  5. imag_part=input()
  6. if real_part == "exit" or imag_part == "exit":
  7. break
  8. real_part = int(real_part)
  9. imag_part = int(imag_part)
  10. list.append((real_part,imag_part))
  11. except ValueError as i:
  12. print("invalid input!")
  13. return list
  14.  
  15.  
  16. def print_complex(a): #in this way we print a complex number
  17. print(str(a[0]) + " + " + str(a[1]) + "i")
  18.  
  19. def print_tuple(list):
  20. for a in list:
  21. print_complex(a)
  22.  
  23. def real(a): #this function returns the real part of a complex number from a tuple
  24. return a[0]
  25.  
  26. def imag(a): #this function returns the imaginary part of a complex number from a tuple
  27. return a[1]
  28.  
  29.  
  30.  
  31. def increasing_real_part(list):#this function returns the longest sequence of numbers with strictly increasing real part
  32. max_nr_elements=0
  33. nr_elements=1
  34. sequence=[]
  35. sequence.append(list[0])
  36. for i in range(len(list)-1):
  37. if real(list[i])>real(list[i+1]):
  38. nr_elements+=1
  39. sequence.append(list[i])
  40. else:
  41. if nr_elements>max_nr_elements:
  42. max_nr_elements=nr_elements
  43. max_sequence=sequence
  44. sequence=[]
  45. nr_elements=0
  46.  
  47. if nr_elements>max_nr_elements:
  48. max_sequence=sequence
  49. return max_sequence
  50.  
  51.  
  52. def modul(c):
  53. return (c[0]*c[0]+c[1]*c[1])**(1/2.0)
  54.  
  55. def same_modules(l):
  56. max_nr_elements=0 #the number of elements of the maximum sequence
  57. begin_index =0
  58. max_begin_index = 0
  59. #sequence.append(l[0]) #we save the first complex number to the current maximum sequence
  60. nr_elements=0 #the number of elements of the current sequence
  61.  
  62. for i in range(len(l)-1):
  63. print(max_begin_index,nr_elements)
  64. if modul(l[i])==modul(l[i+1]):
  65. nr_elements+=1
  66.  
  67. else:
  68. if nr_elements > max_nr_elements:
  69. max_nr_elements=nr_elements
  70. max_begin_index = begin_index
  71. begin_index= i
  72. nr_elements=0
  73.  
  74. return l[max_begin_index:max_begin_index+max_nr_elements]
  75.  
  76. def print_sequence(sequence):
  77. print_tuple(sequence)
  78.  
  79. def print_same_modules(list):
  80. max_sequence=same_modules(list)
  81. print_sequence(max_sequence)
  82.  
  83. def print_increasing_real_part(list):
  84. max_sequence=increasing_real_part(list)
  85. print_sequence(max_sequence)
  86.  
  87. def run():
  88. list=[(1,2),(1,3),(3,1),(0,2),(7,5),(9,10),(10,9),(14,90),(1,12),(7,9)]
  89. commands={"1":read_a_list, "2":print_tuple, "3":print_same_modules, "4":print_increasing_real_part}
  90. while True:
  91. 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.")
  92. if command=="exit":
  93. return
  94. if command in commands:
  95. commands[command](list)
  96. else:
  97. print("Illegal command!")
  98.  
  99. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement