• API
• FAQ
• Tools
• Archive
SHARE
TWEET # khu a guest Oct 21st, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def set_real_part(number,rpart):
2.     number["real_part"]=rpart
3. def get_real_part(number):
4.     return number["real_part"]
5. def set_imaginary_part(number,ipart):
6.     number["imaginary_part"]=ipart
7. def get_imaginary_part(number):
8.     return number["imaginary_part"]
9.
10.
11. def create_number(rpart, ipart):
12.     '''
13.    Create a number
14.    input:
15.       rpart-the real part
16.       ipart- the imaginary part
17.    output: n- the list with the new number added into it
18.    '''
19.     n={}
20.     set_real_part(n,rpart)
21.     set_imaginary_part(n,ipart)
22.     return n
23.
25.     '''
27.    '''
29.     print("2.Display the list")
30.     print("3.Display the longest sequence of numbers with increasing real part")
31.     print("4.Display the longest sequence of numbers having their modulus in the [0,10] range")
32.     print("5.Exit")
33.
34. def show_numbers(data):
35.     '''
36.    Display the entire list of numbers
37.    input:
38.       data- the list
39.    output: print the numbers from list
40.    '''
41.     print("List of numbers")
42.     for n in data:
43.         print("["+str(get_real_part(n))+","+str(get_imaginary_part(n))+"]")
44.
45. def init_number_list():
46.     '''
47.    Add the following numbers to the list
48.    input:
49.        res-an empty list
50.    output:
51.        res-the final list
52.    '''
53.     res=[]
54.     res.append(create_number(2,7))
55.     res.append(create_number(-1,3))
56.     res.append(create_number(2,7))
57.     res.append(create_number(7,3))
58.     res.append(create_number(1,7))
59.     res.append(create_number(3,1))
60.     res.append(create_number(4,5))
61.     res.append(create_number(11,-6))
62.     res.append(create_number(19,-3))
63.     res.append(create_number(-3,3))
64.     return res
65.
67.     '''
69.    input: the real and the imaginary part
70.    output: the introduced number
71.    '''
72.     rpart=int(input("enter the real part: "))
73.     ipart=int(input("enter the imaginary part: "))
74.     return create_number(rpart, ipart)
75.
77.     '''
79.    params:
80.       numbers- the list
81.    output: add the number to the list
82.    '''
84.     numbers.append(n)
85.
86. def increasing_real_part(numbers):
87.     '''
88.    Find the longest sequence of numbers having increasing modulus.
89.    input: numers - the list
90.    output: l - a list containing the longest sequence of numbers with increasing real part
91.    '''
92.     l=[]
93.     s=0
94.     max=0
95.     i=0
96.     while i<len(numbers)-1:
97.         if get_real_part(numbers[i])<get_real_part(numbers[i+1]):
98.             s+=1
99.             if s>=max:
100.                 max=s+1
101.                 position=i-s+1
102.         else:
103.             s=0
104.         i+=1
105.     while max!=0:
106.         l.append(create_number(get_real_part(numbers[position]),get_imaginary_part(numbers[position])))
107.         position+=1
108.         max-=1
109.     return l
110.
111. def modulus_in_range(numbers):
112.     '''
113.    Find the longest sequence of numbers having their modulus in the [0,10] range.
114.    input: numbers - the list
115.    output: l2 - a list containing the longest sequence of numbers having their modulus in the [0,10] range
116.    '''
117.     import math
118.     l2=[]
119.     s=0
120.     max=0
121.     i=0
122.     while i<len(numbers):
123.         mod=math.sqrt(get_real_part(numbers[i])*get_real_part(numbers[i])+get_imaginary_part(numbers[i])*get_imaginary_part(numbers[i]))
124.         if mod>=0 and mod<=10:
125.             s+=1
126.             if s>max:
127.                 max=s
128.                 position=i-s+1
129.         else:
130.             s=0
131.         i+=1
132.     while max!=0:
133.         l2.append(create_number(get_real_part(numbers[position]),get_imaginary_part(numbers[position])))
134.         position+=1
135.         max-=1
136.     return l2
137.
138. def start():
139.     numbers=init_number_list()
140.     while True:
142.         choice=input(">")
143.         if choice == "1":
145.         elif choice == '2':
146.             show_numbers(numbers)
147.         elif choice=='3':
148.             result=increasing_real_part(numbers)
149.             show_numbers(result)
150.         elif choice=='4':
151.             result2=modulus_in_range(numbers)
152.             show_numbers(result2)
153.         elif choice == '5':
154.             break
155.         else: