Guest User

fortinet-oa

a guest
Jun 29th, 2025
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. import asyncio
  2. import concurrent.futures
  3.  
  4. def blocking_io():
  5. with open('test.txt', 'w') as f:
  6. for i in range(10000):
  7. f.write('test')
  8. return 'io completed'
  9.  
  10. async def main():
  11. with concurrent.futures.ThreadPoolExecutor() as executor:
  12. loop = asyncio.get_event_loop()
  13. result = await loop.run_in_executor(executor, blocking_io)
  14. print(result)
  15.  
  16. if __name__ == "__main__":
  17. asyncio.run(main())
  18.  
  19. Which of the following modifications will NOT allow the program to work as intended?
  20.  
  21. Pick ONE option
  22.  
  23. Replace concurrent.futures.ThreadPoolExecutor() with concurrent.futures.ProcessPoolExecutor()
  24. Replace loop = asyncio.get_event_loop() with loop = asyncio.new_event_loop()
  25. Replace result = await loop.run_in_executor(executor, blocking_io) with result = blocking_io()
  26. Replace asyncio.run(main()) with main()
  27.  
  28. --------
  29.  
  30. from abc import ABC, abstractmethod
  31.  
  32. class Quacker(ABC):
  33. @abstractmethod
  34. def quack(self):
  35. pass
  36.  
  37. class Duck:
  38. def quack(self):
  39. return "Quack!"
  40.  
  41. class Cat:
  42. def quack(self):
  43. return "Meow!"
  44.  
  45. def is_a_quacker(obj):
  46. if type(obj) is Quacker:
  47. return True
  48. return False
  49.  
  50. Quacker.register(Duck)
  51.  
  52. duck = Duck()
  53. cat = Cat()
  54.  
  55. which of the following code snippets will return True?
  56.  
  57. Pick ONE option
  58.  
  59. print(is_a_quacker(duck))
  60. print(is_a_quacker(cat))
  61. print(issubclass(Duck, Quacker))
  62. print(issubclass(Cat, Quacker))
  63.  
  64. --------
  65.  
  66. def count_up_to(n):
  67. count = 0
  68. for _ in range(n):
  69. count += 1
  70. return count
  71.  
  72.  
  73. which one of the following versions of the main() function would decrease the total execution time of count_up_to?
  74.  
  75. Pick ONE option
  76.  
  77. ----
  78.  
  79. import threading
  80. def main():
  81. n = 10 ** 7
  82. t1 = threading.Thread(target=count_up_to, args=(n,))
  83. t2 = threading.Thread(target=count_up_to, args=(n,))
  84. t1.start()
  85. t2.start()
  86. t1.join()
  87. t2.join()
  88.  
  89. ----
  90.  
  91. import multiprocessing
  92. def main():
  93. n = 10 ** 7
  94. p1 = multiprocessing.Process(target=count_up_to, args=(n,))
  95. p2 = multiprocessing.Process(target=count_up_to, args=(n,))
  96. p1.start()
  97. p2.start()
  98. p1.join()
  99. p2.join()
  100.  
  101. ----
  102.  
  103. def main():
  104. n = 10 ** 7
  105. count_up_to(n)
  106. count_up_to(n)
  107.  
  108. ----
  109.  
  110. import threading
  111. import time
  112.  
  113. def main():
  114. n = 10 ** 7
  115. t1 = threading.Thread(target=count_up_to, args=(n,))
  116. t2 = threading.Thread(target=count_up_to, args=(n,))
  117. t1.start()
  118. t2.start()
  119. t1.join()
  120. t2.join()
  121. time.sleep(1)
  122.  
  123. --------
  124.  
  125. import threading
  126.  
  127. class SharedCounter:
  128. def __init__(self):
  129. self.value = 0
  130. self._value_lock = threading.Lock()
  131.  
  132. def increment(self, delta=1):
  133. with self._value_lock:
  134. self.value += delta
  135.  
  136. def get_value(self):
  137. with self._value_lock:
  138. return self.value
  139.  
  140. def worker(counter, num_iters):
  141. for _ in range(num_iters):
  142. counter.increment()
  143.  
  144. if __name__ == "__main__":
  145. counter = SharedCounter()
  146. num_iters = 10000
  147. num_workers = 5
  148. threads = [threading.Thread(target=worker, args=(counter, num_iters)) for _ in range(num_workers)]
  149. for t in threads:
  150. t.start()
  151. for t in threads:
  152. t.join()
  153. print(counter.get_value())
  154.  
  155. Pick ONE option
  156.  
  157. It is guaranteed to print 50000
  158. It may print a number less than 50000
  159. It may print a number more than 50000
  160. The program may raise a threading-related exception
  161.  
  162. --------
  163.  
  164. Madam C.J. Walker's Business Plan
  165.  
  166. Madam C.J. Walker, known as the first African-American businesswoman in America, had a business model involving the sale of cosmetics and perfumes for women, which can be likened to a modified 0-1 knapsack problem.
  167.  
  168. There are n different products available for purchase and resale. The cost of the i item is given by cost[i], and it can be resold for a profit of 2 ** i
  169.  
  170. Given the total available to x to invest, determine the maximum profit that can be generated with the given amount of money. Since the answer can be quite large, return the result modulo (10 ** 9 + 7).
  171.  
  172. Example
  173. Suppose there are n = 5 items with costs cost = [10, 20, 14, 40, 50], and Walker's initial amount of money is x = 70.
  174.  
  175. Some combinations of items (0-based indexing) Walker can buy are:
  176. - Items 0, 1, and 2 for a cost of 44 and obtain a profit of 2 ** 0 + 2 ** 1 + 2 ** 2 = 7
  177. - Items 0 and 4 for a cost of 60 and obtain a profit of 2 ** 0 + 2 ** 4 = 17
  178. - Items 1 and 4 for a cost of 70 and obtain a profit of 2 ** 1 + 2 ** 4 = 18
  179. - Items 2 and 4 for a cost of 64 and obtain a profit of 2 ** 2 + 2 ** 4 = 20
  180.  
  181. Out of all the possible combinations, the maximum profit is 20 when purchasing items 2 and 4 for a cost of 64.
  182.  
  183. def calculateMaximumProfit(cost, x):
  184.  
  185. --------
  186.  
  187. K-Means Clustering
  188.  
  189. In a k-means clustering problem, a dataset contains n data points, where i data point is represented by the feature vector location[i], The goal is to create k clusters. where the cluster centers or the cluster centroids can be placed at any point in the feature space. The overall quality of the clustering is measured by the maximum distance between any data point and its nearest cluster center.
  190.  
  191. The best possible quality is achieved by optimally placing the cluster centers to minimize this maximum distance. Determine this maximum distance between any data point and its nearest cluster center.
  192.  
  193. Note: The distance between two feature points x and y is defined as |x - y|. where |x| denotes the absolute value of x.
  194.  
  195. Example
  196. n = 5
  197. location =[4, 1, 6, 7, 2]
  198. k = 2
  199.  
  200. Let the cluster centers be placed at points 3 and 7.
  201.  
  202. Current Location | Closest Cluster Center | Distance
  203. 4 | 3 | |4 - 3| = 1
  204. 1 | 3 | |1 - 3| = 2
  205. 6 | 7 | |6 - 7| = 1
  206. 7 | 7 | |7 - 7| = 0
  207. 2 | 3 | |2 - 3| = 1
  208.  
  209. Hence, the maximum of all distances is 2.
  210.  
  211. def getMaximumDistance(location, k):
  212.  
  213. --------
  214.  
  215. Signal Pings
  216.  
  217. You have an array of n binary signals, where each signal initially has a value of 0. There are n different pings made to these signals, changing their value from 0 to 1. The i ping affects the signal at index ping[i].
  218.  
  219. After each ping, the processor sorts the array by performing sweeps from left to right, swapping adjacent elements where signal[i] = 1 and signal[i + 1] = 0. The processor stops when no swaps are made in a sweep.
  220.  
  221. Determine the number of sweeps required after each ping to sort the array.
  222.  
  223. Note: Each signal is only pinged once.
  224.  
  225. Example
  226.  
  227. Given n = 4. ping =[1, 2, 4, 3]
  228.  
  229. Ping | Operations required to process signals | signal after each Ping
  230. 1 | Only one sweep is sufficient to sort | [1, 0, 0, 0]
  231. | the array into [0, 0, 0, 1]. One other |
  232. | sweep is run with no swaps. Total |
  233. | sweeps = 2. |
  234. 2 | 2 sweeps will sort the array. | [1, 1, 0, 0]
  235. | Similarly, another sweep with no |
  236. | swaps. Total sweeps = 3. |
  237. 3 | 2 sweeps will sort the array. | [1, 1, 0, 1]
  238. | Similarly, another sweep with no |
  239. | swaps. Total sweeps = 3. |
  240. 4 | signal is already sorted. So, just one | [1, 1, 1, 1]
  241. | sweep with no swaps. Total sweeps |
  242. | = 1. |
  243.  
  244. sweeps = [2, 3, 3, 1]
  245.  
  246.  
  247. def getRequiredSweeps(ping):
  248.  
Advertisement
Add Comment
Please, Sign In to add comment