BVB Source Codes

Algorithm-Implementations Show Counterfeit_Coin_Problem_Solution.py Source code

Return Download Algorithm-Implementations: download Counterfeit_Coin_Problem_Solution.py Source code - Download Algorithm-Implementations Source code - Type:.py
  1. __author__ = "Nico Hinderling"
  2. __email__ = "nhinderl@bu.edu"
  3. __site__ = "nicohinderling.me"
  4.  
  5. '''
  6. I wrote out an explanation in the README, yet just in case:
  7. Another useful resource to understand the logic:
  8.    http://mathforum.org/library/drmath/view/55618.html
  9.  
  10. I hope this helps!
  11. '''
  12.  
  13. import random
  14. #============Set up============#
  15. counterfeit = random.randint(0,11)
  16. counterValue = random.randint(0,1)
  17. coins = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
  18. coins[counterfeit] = 2 + 2*(counterValue)
  19. coinLocation = 0
  20. weight = "?"
  21.  
  22. #============Case Explanation============#
  23. #Uncomment to check with original values
  24. '''
  25. if(counterValue == 0):
  26.    print("The counterfeit value is " + str(counterfeit) + " and Lighter \n")
  27. else:
  28.    print("The counterfeit value is " + str(counterfeit) + " and Heavier \n")
  29. print(str(coins) +  "\n")
  30. '''
  31.  
  32. #============Solution============#
  33. #First Weighing Groups
  34. w1groupA = coins[0] + coins[1] + coins[2] + coins[3]
  35. w1groupB = coins[4] + coins[5] + coins[6] + coins[7]
  36. #Second Weighing Groups
  37. w2groupA = coins[0] + coins[1] + coins[2]
  38. w2groupB = coins[8] + coins[9] + coins[10]
  39. w2groupC = coins[0] + coins[4] + coins[5]
  40. w2groupD = coins[1] + coins[6] + coins[7]
  41. w2groupE = coins[4] + coins[0] + coins[1]
  42. w2groupF = coins[5] + coins[2] + coins[3]
  43.  
  44. #====Logic====#
  45. if(w1groupA == w1groupB):               #Weigh One
  46.     if(w2groupA == w2groupB):               #Weigh Two
  47.         if(coins[11] > coins[0]):               #Weigh Three
  48.             coinLocation = 11
  49.             weight = "Heavier"
  50.         else:                                   #Weigh Three
  51.             coinLocation = 11
  52.             weight = "Lighter"
  53.     elif(w2groupA < w2groupB):              #Weigh Two
  54.         if(coins[8] == coins[9]):               #Weigh Three
  55.             coinLocation = 10
  56.             weight = "Heavier"
  57.         elif(coins[8] > coins[9]):              #Weigh Three
  58.             coinLocation = 8
  59.             weight = "Heavier"
  60.         else:                                   #Weigh Three
  61.             coinLocation = 9
  62.             weight = "Heavier"
  63.     else:                                   #Weigh Two
  64.         if(coins[8] == coins[9]):               #Weigh Three
  65.             coinLocation = 10
  66.             weight = "Lighter"
  67.  
  68.         elif(coins[8] < coins[9]):              #Weigh Three
  69.             coinLocation = 8
  70.             weight = "Lighter"
  71.         else:                                   #Weigh Three
  72.             coinLocation = 9
  73.             weight = "Lighter"
  74.  
  75. elif(w1groupA < w1groupB):              #Weigh One
  76.     if(w2groupC == w2groupD):               #Weigh Two
  77.         if(coins[8] == coins[2]):               #Weigh Three
  78.             coinLocation = 3
  79.             weight = "Lighter"
  80.         else:                                   #Weigh Three
  81.             coinLocation = 2
  82.             weight = "Lighter"
  83.     elif(w2groupC < w2groupD):              #Weigh Two
  84.         if(coins[6] == coins[7]):               #Weigh Three
  85.             coinLocation = 0
  86.             weight = "Lighter"
  87.         elif(coins[6] > coins[7]):              #Weigh Three
  88.             coinLocation = 6
  89.             weight = "Heavier"
  90.         else:                                   #Weigh Three
  91.             coinLocation = 7
  92.             weight = "Heavier"
  93.     else:                                   #Weigh Two
  94.         if(coins[4] == coins[5]):               #Weigh Three
  95.             coinLocation = 1
  96.             weight = "Lighter"
  97.         elif(coins[4] > coins[5]):              #Weigh Three
  98.             coinLocation = 4
  99.             weight = "Heavier"
  100.         else:                                   #Weigh Three
  101.             coinLocation = 5
  102.             weight = "Heavier"
  103. else:                                   #Weigh One
  104.     if(w2groupE == w2groupF):               #Weigh Two
  105.         if(coins[8] == coins[6]):               #Weigh Three
  106.             coinLocation = 7
  107.             weight = "Lighter"
  108.         else:                                   #Weigh Three
  109.             coinLocation = 6
  110.             weight = "Lighter"
  111.     elif(w2groupE < w2groupF):              #Weigh Two
  112.         if(coins[2] == coins[3]):               #Weigh Three
  113.             coinLocation = 4
  114.             weight = "Lighter"
  115.         elif(coins[2] > coins[3]):              #Weigh Three
  116.             coinLocation = 2
  117.             weight = "Heavier"
  118.         else:                                   #Weigh Three
  119.             coinLocation = 3
  120.             weight = "Heavier"
  121.     else:                                   #Weigh Two
  122.         if(coins[0] == coins[1]):               #Weigh Three
  123.             coinLocation = 5
  124.             weight = "Lighter"
  125.         elif(coins[0] > coins[1]):              #Weigh Three
  126.             coinLocation = 0
  127.             weight = "Heavier"
  128.         else:                                   #Weigh Three
  129.             coinLocation = 1
  130.             weight = "Heavier"
  131.  
  132. #============Test============#
  133.  
  134. if(counterValue == 0 and weight == "Lighter" and counterfeit == coinLocation):
  135.     print("Correct! The Counterfeit Coin is Coin " + str(coinLocation) + " and is " + str(weight))
  136. elif(counterValue == 1 and weight == "Heavier" and counterfeit == coinLocation):
  137.     print("Correct! The Counterfeit Coin is Coin " + str(coinLocation) + " and is " + str(weight))
  138. else:
  139.     print("Index is " + str(coinLocation))
  140.     print("Weight is " + str(weight))
  141.     print("Case " + str(case))
downloadCounterfeit_Coin_Problem_Solution.py Source code - Download Algorithm-Implementations Source code
Related Source Codes/Software:
raty - 2017-04-22
RDVTabBarController - Highly customizable tabBar and tabBarController fo... 2017-04-22
material-icon-lib - Library containing over 1500 material vector icons... 2017-04-21
httpdiff - Perform the same request against two HTTP servers ... 2017-04-21
jquerytools - The missing UI library for the Web ... 2017-04-21
mcrouter - Mcrouter is a memcached protocol router for scalin... 2017-04-22
dynomite - A generic dynamo implementation for different k-v ... 2017-04-22
kityminder - Baidu brain figure 2017-04-22
llvm - Mirror of official llvm git repository located at ... 2017-04-22
RBBAnimation - Block-based animations made easy, comes with easin... 2017-04-22
ied - 2017-04-29
Nimble - A Matcher Framework for Swift and Objective-C 2017-04-29
MHVideoPhotoGallery - A Photo and Video Gallery 2017-04-29
shoulda-matchers - Collection of testing matchers extracted from Shou... 2017-04-29
Android-SlideExpandableListView - A better ExpandableListView, with animated expanda... 2017-04-29
AppSales-Mobile - App Sales allows iPhone and Mac App Store develope... 2017-04-29
react-templates - Light weight templates for react ... 2017-04-28
afterglow-theme - A minimal dark Theme for Sublime Text 2 and 3 2017-04-28
jwt-go - Golang implementation of JSON Web Tokens (JWT) 2017-04-28
DeerResume - Tool MarkDown online resume, online preview, edit,... 2017-04-28

 Back to top