Advertisement
ZergRushA

RT-AI-1

Sep 12th, 2022 (edited)
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1. '''
  2. Task 1.3
  3.  
  4. x = 5 >= 2
  5. A = {1,3,7,8}
  6. B = {2,5,4,10, 'apple'}
  7. C = A & B
  8. df  = 'Nicolas Coppolo', 34, 'g'
  9. z = 'type'
  10. D = ['1', 'title', 2, 'content']
  11.  
  12. print(type(x),
  13.     type(A),
  14.     type(B),
  15.     type(C),
  16.     type(df),
  17.     type(z),
  18.     type(D), sep='\n')
  19.  
  20.  
  21. Task 2.3
  22. def interval(num):
  23.     if num < -5: return f'{num} is in interval: (-inf, -5)'
  24.     elif num > 5: return f'{num} is in interval: (5, +inf)'
  25.     elif num in range(-5, 6): return f'{num} is in interval: (-5, 5)'
  26.  
  27.  
  28. def main():
  29.     x = int(input('Enter a number: '))
  30.     print(interval(x))
  31.  
  32. if __name__ == '__main__':
  33.     main()
  34.  
  35.  
  36.  
  37. Task 3.3.1
  38. x = 10
  39. while x > 0:
  40.     print(x)
  41.     x -= 3
  42.  
  43.  
  44.  
  45. Task 3.3.2
  46. # position = должность
  47. list_of_characteristics = ['age', 'sex', 'height',
  48.                             'weight', 'position']
  49.  
  50. for characteristic in list_of_characteristics:
  51.     print(characteristic)
  52.  
  53.  
  54.  
  55. Task 3.3.3
  56. list_of_digits = list(range(2, 16))
  57. print(list_of_digits)
  58.  
  59.  
  60.  
  61. Task 3.3.4
  62. for i in range(105, 0, -25):
  63.     print(i)
  64.  
  65.  
  66.  
  67. Task 3.3.5
  68. # [0,1,2,3,4,5,6,7,8,9] ---> [8,1,6,3,4,5,2,7,0,9]
  69.  
  70. def swap(digits):
  71.     digits[0::2] = reversed(digits[0::2])
  72.     return digits
  73.  
  74. test = [0,1,2,3,4,5,6,7,8,9]
  75. print(swap(test))
  76.  
  77.  
  78.  
  79. Task 4.3.1
  80. import random
  81. from matplotlib import pyplot as plt
  82. array_of_rands = [random.uniform(0, 1) for _ in range(10)]
  83.  
  84. average = sum(array_of_rands)/len(array_of_rands)
  85.  
  86. sorted_array = sorted(array_of_rands)
  87.  
  88. median = (sorted_array[4]+sorted_array[5])/2
  89.  
  90. print(f'Array: {array_of_rands}',
  91.     f'Average value: {average}',
  92.     f'Median value: {median}')
  93. plt.scatter(array_of_rands, array_of_rands)
  94. plt.gca().set(xlim=(0, 1), ylim=(0, 1))
  95. plt.show()
  96.  
  97.  
  98.  
  99. Task 4.3.2
  100. import math
  101. from matplotlib import pyplot as plt
  102.  
  103.  
  104. def calc_func(x):
  105.     form_1 = math.sqrt(1+math.exp(math.sqrt(x)) + (math.cos(x))**2)
  106.     form_2 = abs((1-(math.sin(x))**3))
  107.     form_3 = math.log(2*x)
  108.  
  109.     return form_1/form_2 + form_3
  110.  
  111. x_digits = list(range(1,11))
  112.  
  113. calc_result = [calc_func(x) for x in x_digits]
  114.  
  115. print(calc_result)
  116. plot 1
  117. plt.subplot(2, 2, 1)
  118. plt.plot(x_digits, calc_result)
  119.  
  120.  
  121. plot 2
  122. plt.subplot(2, 2, 2)
  123. plt.scatter(x_digits[:5], calc_result[:5])
  124.  
  125.  
  126.  
  127. plt.show()
  128.  
  129.  
  130.  
  131. Task 4.3.3
  132.  
  133. from matplotlib import pyplot as plt
  134. from scipy.integrate import simps
  135. import numpy as np
  136.  
  137. x = np.arange(0, 10, 1)
  138. y = np.abs(np.cos(x*np.exp(np.cos(x) + np.log(x+1))))
  139.  
  140. plt.grid()
  141. plt.plot(x, y, c='r')
  142. plt.fill_between(x, y)
  143. plt.show()
  144.  
  145. area = np.trapz(y)
  146. print(area)
  147.  
  148.  
  149.  
  150. Task 4.3.4
  151. from matplotlib import pyplot as plt
  152.  
  153. month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
  154.  
  155. microsoft = [231.96, 232.38, 235.77, 252.18, 249.68, 270.90, 284.91, 301.88, 281.92, 331.62, 330.59, 336.32]
  156.  
  157. google =  [131.96, 121.26, 122.15, 131.46, 124.61, 136.96, 145.86, 151.83, 141.50, 149.80, 165.30, 177.57]
  158.  
  159. apple =  [91.37, 101.10, 103.13, 117.68, 117.84, 122.09, 134.73, 144.70, 133.68, 148.05, 141.90, 144.85]
  160.  
  161. fig, ax = plt.subplots()
  162.  
  163.  
  164. ax.plot(month, microsoft, label='Microsoft')
  165. ax.plot(month, apple, label='Apple')
  166. ax.plot(month, google, label='Google')
  167.  
  168. ax.legend()
  169.  
  170. fig.set_figheight(5)
  171. fig.set_figwidth(8)
  172.  
  173. plt.show()
  174.  
  175.  
  176. Task 4.3.5
  177. import math
  178. commands = {'+': lambda x, y: x+y,
  179.             '-': lambda x, y: x-y,
  180.             '*': lambda x, y: x*y,
  181.             '%': lambda x, y: x/y,
  182.             'e': lambda x, y: math.exp(x+y),
  183.             'sin': lambda x, y: math.sin(x+y),
  184.             'cos': lambda x, y: math.cos(x+y),
  185.             'pow': lambda x, y: x**y}
  186.  
  187.  
  188. def main():
  189.     print(*['Plus: +', 'Minus: -', 'Multiply: *', 'Divide: %', 'Exponential: e', 'Sin: sin', 'Cos: cos', 'Pow: pow'], sep='\n')
  190.     print('---------')
  191.     expression = input('Choose one of these operands: ')
  192.     x, y = map(int, input('Now write two numbers: ').split())
  193.  
  194.     return commands[expression](x, y)
  195.    
  196.  
  197. if __name__ == '__main__':
  198.     print(main())
  199.  
  200.  
  201. '''
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement