Advertisement
Radeen10-_

Closest and Smallest String

Dec 21st, 2021
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. Input
  2. a string strng of n positive numbers (n = 0 or n >= 2)
  3. Let us call weight of a number the sum of its digits. For example 99 will have "weight" 18, 100 will have "weight" 1.
  4.  
  5. Two numbers are "close" if the difference of their weights is small.
  6.  
  7. Task:
  8. For each number in strng calculate its "weight" and then find two numbers of strng that have:
  9.  
  10. the smallest difference of weights ie that are the closest
  11. with the smallest weights
  12. and with the smallest indices (or ranks, numbered from 0) in strng
  13. Output:
  14. an array of two arrays, each subarray in the following format:
  15. [number-weight, index in strng of the corresponding number, original corresponding number in strng]
  16.  
  17. or a pair of two subarrays (Haskell, Clojure, FSharp) or an array of tuples (Elixir, C++)
  18.  
  19. or a (char*) in C or a string in some other languages mimicking an array of two subarrays or a string
  20.  
  21. or a matrix in R (2 rows, 3 columns, no columns names)
  22.  
  23. The two subarrays are sorted in ascending order by their number weights if these weights are different, by their indexes in the string if they have the same weights.
  24.  
  25. Examples:
  26. Let us call that function closest
  27.  
  28. strng = "103 123 4444 99 2000"
  29. the weights are 4, 6, 16, 18, 2 (ie 2, 4, 6, 16, 18)
  30.  
  31. closest should return [[2, 4, 2000], [4, 0, 103]] (or ([2, 4, 2000], [4, 0, 103])
  32. or [{2, 4, 2000}, {4, 0, 103}] or ... depending on the language)
  33. because 2000 and 103 have for weight 2 and 4, their indexes in strng are 4 and 0.
  34. The smallest difference is 2.
  35. 4 (for 103) and 6 (for 123) have a difference of 2 too but they are not
  36. the smallest ones with a difference of 2 between their weights.
  37. ....................
  38.  
  39. strng = "80 71 62 53"
  40. All the weights are 8.
  41. closest should return [[8, 0, 80], [8, 1, 71]]
  42. 71 and 62 have also:
  43. - the smallest weights (which is 8 for all)
  44. - the smallest difference of weights (which is 0 for all pairs)
  45. - but not the smallest indices in strng.
  46. ....................
  47.  
  48. strng = "444 2000 445 544"
  49. the weights are 12, 2, 13, 13 (ie 2, 12, 13, 13)
  50.  
  51. closest should return [[13, 2, 445], [13, 3, 544]] or ([13, 2, 445], [13, 3, 544])
  52. or [{13, 2, 445}, {13, 3, 544}] or ...
  53. 444 and 2000 have the smallest weights (12 and 2) but not the smallest difference of weights;
  54. they are not the closest.
  55. Here the smallest difference is 0 and in the result the indexes are in ascending order.
  56. ...................
  57.  
  58. closest("444 2000 445 644 2001 1002") --> [[3, 4, 2001], [3, 5, 1002]] or ([3, 4, 2001],
  59. [3, 5, 1002]]) or [{3, 4, 2001}, {3, 5, 1002}] or ...
  60. Here the smallest difference is 0 and in the result the indexes are in ascending order.
  61. ...................
  62.  
  63. closest("239382 162 254765 182 485944 468751 49780 108 54")
  64. The weights are: 27, 9, 29, 11, 34, 31, 28, 9, 9.
  65. closest should return  [[9, 1, 162], [9, 7, 108]] or ([9, 1, 162], [9, 7, 108])
  66. or [{9, 1, 162}, {9, 7, 108}] or ...
  67. 108 and 54 have the smallest difference of weights too, they also have
  68. the smallest weights but they don't have the smallest ranks in the original string.
  69. ..................
  70.  
  71. closest("54 239382 162 254765 182 485944 468751 49780 108")
  72. closest should return  [[9, 0, 54], [9, 2, 162]] or ([9, 0, 54], [9, 2, 162])
  73. or [{9, 0, 54}, {9, 2, 162}] or ...
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. CODE::::
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. def closest(strng):
  90.    if (strng == ""): return []
  91.    x= strng.split()
  92.    lst=[]
  93.    lst1=[]
  94.    for index,value in enumerate(x):
  95.        value=int(value)
  96.        y=sum(int(digit) for digit in str(value))
  97.        lst.append([y,index,value])
  98.    lst=sorted(lst)
  99.    d = min(y[0] - x[0] for x, y in zip(lst, lst[1:]))
  100.    for x,y in zip(lst,lst[1:]):
  101.        x1=abs(x[0]-y[0])
  102.        x2=[x,y,x1]
  103.        lst1.append(x2)
  104.    for ele in lst1:
  105.        for ele1 in ele:
  106.            ele1=str(ele1)
  107.    min1 = lst1[0][2]
  108.    minIx = 0
  109.    for i in range(len(lst1)):
  110.  
  111.        # If the other element is min than first element
  112.        if lst1[i][2] < min1:
  113.            min1 = lst1[i][2]  # It will change
  114.            minIx = i
  115.    x=lst1[minIx].pop()
  116.    return lst1[minIx]
  117.  
  118.  
  119. print(closest("403749 18 278325 97 304194 119 58359 165 144403 128 38 "))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement