├── BrainFibo.py ├── LifeIsHard.py ├── LookAndSayBinary.py ├── .gitignore ├── TrickyPrinting.py ├── SumAB.py ├── README.md ├── SummingUp.py ├── ReverseString.py ├── ParseProblemsList.py ├── JosephusProblem.py ├── SumInLoop.py ├── ArithmeticProgression.py ├── ArrayChecksum.py ├── SumsInLoop.py ├── FahrenheitToCelsius.py ├── SquareRoot.py ├── CollatzSequence.py ├── SumOfDigits.py ├── BubbleSort.py ├── BullsAndCows.py ├── CombinationsCounting.py ├── RotateString.py ├── FoolsDay2014.py ├── Rounding.py ├── WeightedSum.py ├── CardNames.py ├── BubbleInArray.py ├── BicycleRace.py ├── LinearCongruentialGenerator.py ├── SortWithIndexes.py ├── NeumannRandomGenerator.py ├── MedianOfThree.py ├── SavingsCalculator.py ├── BitCount.py ├── MortgageCalculator.py ├── DiceRolling.py ├── PythagoreanTriples.py ├── CardsShuffling.py ├── FibonacciDivisibility.py ├── LinearEquation.py ├── ParityControl.py ├── PythagoreanTheorem.py ├── Average.py ├── MinimumOfThree.py ├── MinimumOfTwo.py ├── test.py ├── Anagrams.py ├── DuelChances.py ├── PrimeNumbersGeneration.py ├── GreatestCommonDivisor.py ├── DoubleDiceRoll.py ├── SelectionSort.py ├── CaesarShiftCipher.py ├── IntegerFactorization.py ├── BodyMassIndex.py ├── ModularCalculator.py ├── TwoPrinters.py ├── Triangles.py ├── GradientCalculation.py ├── Palindromes.py ├── QuadraticEquation.py ├── LICENSE ├── ArrayCounters.py ├── SmoothingTheWeather.py ├── BinarySearch.py ├── AzimuthAtTreasureIsland.py ├── Benchmark.py ├── VowelCount.py ├── QuickSort.py ├── TriangleArea.py ├── MatchingWords.py ├── LifeIsSimple.py ├── FourPicsOneWord.py ├── InsertionSort.py ├── BezierCurve.py ├── MatchingBrackets.py ├── FibonacciDivisibilityAdvanced.py ├── TupperSelfReferentialFormula.py ├── MaximumOfArray.py ├── TicTacToe.py ├── FlyingTextScreensaver.py ├── SweetHarvest.py ├── RockPaperScissors.py ├── BrainfuckInterpreter.py ├── LuhnAlgorithm.py ├── RotationIn2DSpace.py ├── FibonacciSequence.py ├── CaesarCipherCracker.py ├── GameOf2048.py ├── WanderingStarSigDiff.py ├── WanderingStar.py └── WanderingStarBroken.py /BrainFibo.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Xzya' 2 | -------------------------------------------------------------------------------- /LifeIsHard.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Xzya' 2 | -------------------------------------------------------------------------------- /LookAndSayBinary.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Xzya' 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | .idea/ 3 | *.png 4 | *.svg -------------------------------------------------------------------------------- /TrickyPrinting.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 54 3 | 4 | +++[>++<-]>[<+++>-]<[>+++<-]>: -------------------------------------------------------------------------------- /SumAB.py: -------------------------------------------------------------------------------- 1 | #input 2 | #7106420 9306007 3 | print(sum(int(i) for i in input().split())) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeAbbeySolutions 2 | Solutions to the Code Abbey problems in python 3 | -------------------------------------------------------------------------------- /SummingUp.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 9 30 3 | >>> +++++ ++++ > +++++ +++++ +++++ +++++ +++++ +++++ < [-<+<<+>>>] > [-<<+<+>>>] <<: -------------------------------------------------------------------------------- /ReverseString.py: -------------------------------------------------------------------------------- 1 | #input 2 | # stay moon bulb keep supper shelf simple yield white 3 | 4 | string = input() 5 | reversed_string = string[::-1] 6 | print(reversed_string) -------------------------------------------------------------------------------- /ParseProblemsList.py: -------------------------------------------------------------------------------- 1 | 2 | import urllib 3 | 4 | url = "http://www.codeabbey.com/index/task_list" 5 | 6 | req = urllib.request.urlopen(url) 7 | html = req.read() 8 | 9 | -------------------------------------------------------------------------------- /JosephusProblem.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 41 4 3 | 4 | def f(n, k): 5 | if n == 1: 6 | return 1 7 | return ((f(n-1, k)+k-1) % n) + 1 8 | 9 | (n, k) = (int(x) for x in input().split()) 10 | survivor = f(n, k) 11 | print(survivor) -------------------------------------------------------------------------------- /SumInLoop.py: -------------------------------------------------------------------------------- 1 | #input 2 | #36 3 | #298 779 777 256 1024 1195 95 196 942 552 712 1080 398 169 754 409 112 625 778 1127 756 192 263 96 1140 884 498 546 977 1174 918 1266 652 395 221 376 4 | input() 5 | print(sum(int(i) for i in input().split())) -------------------------------------------------------------------------------- /ArithmeticProgression.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 8 3 | # 25 11 41 4 | # 16 12 76 5 | # 24 8 83 6 | # 13 1 60 7 | # 24 10 41 8 | # 5 13 89 9 | # 15 18 17 10 | # 15 3 57 11 | 12 | n = int(input()) 13 | 14 | for i in range(0, n): 15 | (a, b, k) = (int(x) for x in input().split()) 16 | result = 0 17 | for j in range(0, k): 18 | result += a + (j * b) 19 | print(result, "", end="") -------------------------------------------------------------------------------- /ArrayChecksum.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 23 3 | # 430 5468 7002 92223 32896 7143 5 7 631791901 707188 63404 7373174 975230312 7909405 45 35 50416821 224836766 43 71 6162421 7896153 10 4 | 5 | n = int(input()) 6 | 7 | numbers = [int(x) for x in input().split()] 8 | result = 0 9 | for num in numbers: 10 | result += num 11 | result *= 113 12 | result %= 10000007 13 | 14 | print(result) -------------------------------------------------------------------------------- /SumsInLoop.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 12 3 | # 620359 178514 4 | # 350027 140040 5 | # 431927 620625 6 | # 391398 983590 7 | # 626951 686743 8 | # 433305 236626 9 | # 141896 398790 10 | # 167039 862348 11 | # 968960 837266 12 | # 782145 311742 13 | # 243770 556701 14 | # 914547 344397 15 | 16 | n = int(input()) 17 | for k in range(0, n): 18 | print(sum(int(i) for i in input().split()), " ", end="") -------------------------------------------------------------------------------- /FahrenheitToCelsius.py: -------------------------------------------------------------------------------- 1 | #input 2 | #35 51 236 154 498 410 178 199 436 499 481 237 403 537 435 38 79 106 545 401 477 562 295 41 437 203 514 196 106 392 352 392 411 557 515 308 3 | 4 | def fahrenheit_to_celsius(f): 5 | return ((f - 32.0) * (5.0/9.0)) 6 | 7 | temps = input().split() 8 | 9 | for i in range(1, int(temps[0]) + 1): 10 | print(round(fahrenheit_to_celsius(int(temps[i]))) ,"", end="") -------------------------------------------------------------------------------- /SquareRoot.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 10 3 | # 98 6 4 | # 5476 10 5 | # 18 12 6 | # 21 9 7 | # 41 11 8 | # 77463 6 9 | # 803 13 10 | # 277 3 11 | # 91029 7 12 | # 149 11 13 | 14 | n = int(input()) 15 | 16 | for i in range(0, n): 17 | (v, steps) = (int(x) for x in input().split()) 18 | 19 | r = 1 20 | for j in range(0, steps): 21 | d = v / r 22 | r = (r + d) / 2 23 | 24 | print(r, "", end="") -------------------------------------------------------------------------------- /CollatzSequence.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 22 3 | # 172 87 19 46558 536 3487 14193 495 16 13633 30 464 3918 21256 2340 45 25538 44 41473 15622 15167 39 4 | 5 | def num_of_steps(x): 6 | counter = 0 7 | while x != 1: 8 | if x % 2 == 0: 9 | x = x / 2 10 | else: 11 | x = 3 * x + 1 12 | counter += 1 13 | return counter 14 | 15 | n = int(input()) 16 | 17 | for x in input().split(): 18 | print(num_of_steps(int(x)), "", end="") -------------------------------------------------------------------------------- /SumOfDigits.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 15 3 | # 308 27 140 4 | # 284 193 192 5 | # 201 250 145 6 | # 176 41 184 7 | # 232 85 1 8 | # 231 276 27 9 | # 271 277 170 10 | # 88 111 34 11 | # 332 145 150 12 | # 339 233 99 13 | # 332 163 117 14 | # 215 74 46 15 | # 200 221 13 16 | # 93 50 40 17 | # 63 220 96 18 | 19 | n = int(input()) 20 | 21 | for k in range(0, n): 22 | a = [int(x) for x in input().split()] 23 | b = a[0] * a[1] + a[2] 24 | print(sum(int(i) for i in str(b)), "", end="") -------------------------------------------------------------------------------- /BubbleSort.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 20 3 | # 18 7 1 8 11 6 5 4 13 20 2 16 9 10 19 14 12 17 15 3 4 | 5 | n = int(input()) 6 | 7 | array = [int(x) for x in input().split()] 8 | 9 | passes = 0 10 | swaps = 0 11 | done = False 12 | while not done: 13 | done = True 14 | passes += 1 15 | for i in range(0, n - 1): 16 | if array[i] > array[i+1]: 17 | done = False 18 | swaps += 1 19 | t = array[i] 20 | array[i] = array[i+1] 21 | array[i+1] = t 22 | 23 | print(passes, swaps) -------------------------------------------------------------------------------- /BullsAndCows.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 1732 13 3 | # 7830 2356 7261 2736 7682 0832 3207 2608 0672 7312 6087 1235 7250 4 | 5 | (number, n) = (x for x in input().split()) 6 | n = int(n) 7 | 8 | guesses = input().split() 9 | 10 | for i in range(0, n): 11 | guess = guesses[i] 12 | 13 | a = 0 14 | b = 0 15 | for c in guess: 16 | if c in number: 17 | if guess.index(c) == number.index(c): 18 | a += 1 19 | else: 20 | b +=1 21 | 22 | print(str(a) + "-" + str(b), "", end="") -------------------------------------------------------------------------------- /CombinationsCounting.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 8 3 | # 62 53 4 | # 96 7 5 | # 104 97 6 | # 90 7 7 | # 109 7 8 | # 103 7 9 | # 75 8 10 | # 113 7 11 | 12 | def factorial(n): 13 | if n <= 1: 14 | return 1 15 | else: 16 | return n * factorial(n-1) 17 | 18 | def c(n, k): 19 | result = factorial(n) / (factorial(k) * factorial(n - k)) 20 | return int(result) 21 | 22 | n = int(input()) 23 | for i in range(0, n): 24 | (n, k) = (int(x) for x in input().split()) 25 | comb = c(n, k) 26 | print(comb, "", end="") -------------------------------------------------------------------------------- /RotateString.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 11 3 | # -2 yecezfzlauaifiy 4 | # -7 pjwjscdtaivraoodlhietd 5 | # 6 ilbfnwuwmijpbeq 6 | # 4 abqdnylslolyqke 7 | # -3 ifivdseequedwefghsitf 8 | # -1 csohemzmrbbvugzeaihq 9 | # 1 kavefyyxbstjwycdrefgyl 10 | # 3 iewbsnlsdjycjpeuc 11 | # 1 pwttoamgajmznda 12 | # -6 stqkwzaavehiyxytl 13 | # -1 biaoboruocdawtlmnghcdpl 14 | 15 | n = int(input()) 16 | 17 | for i in range(0, n): 18 | (k, s) = (input().split()) 19 | k = int(k) 20 | 21 | s = s[k:] + s[:k] 22 | 23 | print(s, "", end="") -------------------------------------------------------------------------------- /FoolsDay2014.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 16 3 | # 2 5 4 | # 2 7 9 14 18 5 | # 2 4 8 13 17 20 6 | # 5 10 15 19 22 25 7 | # 5 7 9 13 15 8 | # 2 4 9 13 18 9 | # 3 8 11 14 10 | # 3 6 8 12 17 11 | # 2 7 9 11 12 | # 3 6 8 11 15 13 | # 1 5 10 14 | # 5 10 12 16 15 | # 3 7 16 | # 5 7 9 11 17 | # 3 5 9 18 | # 5 10 15 18 21 24 19 | 20 | n = int(input()) 21 | 22 | for i in range(0, n): 23 | a = [int(x) for x in input().split()] 24 | 25 | result = 0 26 | for k in range(0, len(a)): 27 | result += a[k] **2 28 | 29 | print(result, "", end="") -------------------------------------------------------------------------------- /Rounding.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 13 3 | # 19443 576 4 | # 6001842 911503 5 | # 4067 1248 6 | # 6121885 186 7 | # -410349 4955307 8 | # 5375848 874 9 | # 2770998 256 10 | # 18765 1284 11 | # 5813 1630 12 | # 8212240 77 13 | # 293304 -4580549 14 | # 7956897 3420372 15 | # 6775023 846 16 | 17 | n = int(input()) 18 | 19 | for k in range(0, n): 20 | num = None 21 | for i in input().split(): 22 | if not(num): 23 | num = float(i) 24 | continue 25 | num /= float(i) 26 | print(round(num)," ", end="") -------------------------------------------------------------------------------- /WeightedSum.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 37 3 | # 7 3599 83815 51913477 10565410 26 1447 2246 166 6 159071 459 429789 123255 143059893 47012 78641 17171 1437 170043 1761720 178 102 62 215393582 36209 3216809 10405 510805 272 234 242276 32012517 39 6841 2497 1745343 4 | 5 | def wsd(digits): 6 | sum = 0 7 | for i in range(0, len(digits)): 8 | sum += int(digits[i]) * (i+1) 9 | return sum 10 | 11 | n = int(input()) 12 | 13 | digits = [i for i in input().split()] 14 | 15 | for k in range(0, n): 16 | print(wsd(digits[k]), "", end="") -------------------------------------------------------------------------------- /CardNames.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 28 3 | # 49 31 32 22 38 5 28 9 12 48 1 37 51 24 39 36 30 20 16 17 45 29 43 25 2 50 27 40 4 | 5 | suits = ['Clubs', 'Spades', 'Diamonds', 'Hearts'] 6 | ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace'] 7 | 8 | n = int(input()) 9 | 10 | cards = [int(x) for x in input().split()] 11 | 12 | for i in range(0, n): 13 | value = cards[i] 14 | rank = value % 13 15 | suit = value // 13 16 | rank = ranks[rank] 17 | suit = suits[suit] 18 | 19 | print(rank + "-of-" + suit, "", end="") 20 | -------------------------------------------------------------------------------- /BubbleInArray.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 637 3371 327 67 924 968 2 6 6 77 5464 21813 70 5 67 74225 46 2 310 6156 50 4 623 87675 1702 3947 4927 9628 7 510 31 65 3321 23993 8406 88 -1 3 | 4 | array = [int(x) for x in input().split()] 5 | array.remove(-1) 6 | 7 | swaps = 0 8 | for i in range(0, len(array) - 1): 9 | if array[i] > array[i+1]: 10 | swaps += 1 11 | t = array[i] 12 | array[i] = array[i+1] 13 | array[i+1] = t 14 | 15 | checksum = 0 16 | for num in array: 17 | checksum += num 18 | checksum *= 113 19 | checksum %= 10000007 20 | 21 | print(swaps, checksum) -------------------------------------------------------------------------------- /BicycleRace.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 23 3 | # 104 17 20 4 | # 151 26 23 5 | # 55 19 15 6 | # 27 28 20 7 | # 14 26 11 8 | # 316 19 15 9 | # 15 24 25 10 | # 48 10 30 11 | # 32 20 18 12 | # 76 10 13 13 | # 13 18 11 14 | # 42 22 23 15 | # 209 24 27 16 | # 10 12 14 17 | # 54 29 25 18 | # 124 25 10 19 | # 22 18 11 20 | # 34 14 22 21 | # 314 24 19 22 | # 138 11 28 23 | # 99 24 23 24 | # 421 28 14 25 | # 104 30 12 26 | 27 | n = int(input()) 28 | 29 | for i in range(0, n): 30 | (s, a, b) = (int(x) for x in input().split()) 31 | 32 | r = s / (a + b) 33 | 34 | point = a * r 35 | print(point, "", end="") -------------------------------------------------------------------------------- /LinearCongruentialGenerator.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 13 3 | # 1261 169 10 6 8 4 | # 131 63712 69 22 11 5 | # 1803 8 3338 2682 23 6 | # 167 3 76 22 19 7 | # 163 411240 55099 35664 13 8 | # 901 9 5 1 22 9 | # 89 9912 40838 35944 21 10 | # 25 4 39548 3869 6 11 | # 197 9620 2323 998 5 12 | # 1079 9 247168 165345 9 13 | # 563 34 15988 2408 23 14 | # 67 98 3914 594 22 15 | # 155 88032 224866 1635 17 16 | 17 | lines = int(input()) 18 | 19 | for i in range(0, lines): 20 | (a, c, m, x0, n) = (int(x) for x in input().split()) 21 | 22 | for j in range(0, n): 23 | x0 = (a * x0 + c) % m 24 | 25 | print(x0, "", end="") -------------------------------------------------------------------------------- /SortWithIndexes.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 19 3 | # 945 426 679 891 378 483 281 141 97 233 848 333 579 639 794 531 46 739 190 4 | 5 | n = int(input()) 6 | 7 | initial_array = [int(x) for x in input().split()] 8 | sorted_array = initial_array[:] 9 | 10 | done = False 11 | while not done: 12 | done = True 13 | for i in range(0, len(sorted_array) - 1): 14 | if sorted_array[i] > sorted_array[i+1]: 15 | done = False 16 | t = sorted_array[i] 17 | sorted_array[i] = sorted_array[i+1] 18 | sorted_array[i+1] = t 19 | 20 | for x in sorted_array: 21 | print(initial_array.index(x) + 1, "", end="") -------------------------------------------------------------------------------- /NeumannRandomGenerator.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 12 3 | # 8090 2968 6239 4655 7581 4410 2312 5929 7019 1855 6186 4514 4 | 5 | n = int(input()) 6 | 7 | numbers = input().split() 8 | 9 | for i in range(0, n): 10 | num = int(numbers[i]) 11 | num_set = [] 12 | iterations = 0 13 | while True: 14 | if num not in num_set: 15 | num_set.append(num) 16 | else: 17 | break 18 | 19 | num **= 2 20 | num = str(num) 21 | 22 | if len(num) < 8: 23 | for j in range(8 - len(num)): 24 | num = "0" + num 25 | num = int(num[2:-2]) 26 | 27 | iterations += 1 28 | 29 | print(iterations, "", end="") -------------------------------------------------------------------------------- /MedianOfThree.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 27 3 | # 596 603 1481 4 | # 8 63 56 5 | # 24 34 16 6 | # 10 28 18 7 | # 62 82 107 8 | # 7 4 14 9 | # 56 62 46 10 | # 25 41 33 11 | # 953 20 876 12 | # 242 9 188 13 | # 93 89 82 14 | # 112 32 109 15 | # 52 33 43 16 | # 141 123 139 17 | # 845 835 671 18 | # 38 8 47 19 | # 10 835 35 20 | # 859 1439 1609 21 | # 671 1640 1543 22 | # 1747 912 904 23 | # 643 705 635 24 | # 450 442 96 25 | # 92 107 450 26 | # 10 20 577 27 | # 8 1 581 28 | # 692 92 158 29 | # 50 46 999 30 | 31 | n = int(input()) 32 | 33 | for k in range(0, n): 34 | line = [int(i) for i in input().split()] 35 | line.sort() 36 | print(line[1], "", end="") -------------------------------------------------------------------------------- /SavingsCalculator.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 20 3 | # 50 550 9 4 | # 1000 8000 1 5 | # 1000 2000 10 6 | # 250 4000 45 7 | # 100 1700 30 8 | # 1000 9000 7 9 | # 250 1500 9 10 | # 1000 13000 5 11 | # 10000 50000 5 12 | # 25 225 10 13 | # 2500 12500 10 14 | # 2500 42500 5 15 | # 10000 170000 15 16 | # 2500 5000 35 17 | # 500 8500 35 18 | # 10000 170000 10 19 | # 1000 11000 2 20 | # 100 400 30 21 | # 500 3500 25 22 | # 1000 14000 9 23 | 24 | n = int(input()) 25 | 26 | for i in range(0, n): 27 | (s, r, p) = (int(x) for x in input().split()) 28 | 29 | years = 0 30 | while s < r: 31 | s *= (1 + p/100) 32 | years += 1 33 | 34 | print(years, "", end="") -------------------------------------------------------------------------------- /BitCount.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 59 3 | # 480 1227875044 -4 -3 67 -5351 141187766 -8509591 16678549 146293 -2889 -170407464 -31 -431289 -69 168 13 833 346 133173075 150 59343 39627598 160 -25227 -4625 -508 111203789 -168186422 1303000 -183 -129798436 -947737 124974 -5097533 -140 196978 163518 -124574976 -46936 7689359 4 -16333 68620218 2 33283 -13 -21726403 -1959 85549 -241 2 93426 6439 988086988 19919714 -11 18751241 374940148 4 | 5 | n = int(input()) 6 | 7 | numbers = [int(x) for x in input().split()] 8 | 9 | for num in numbers: 10 | bits = bin(num) 11 | if (num >= 0): 12 | print(bits.count("1"), "", end="") 13 | else: 14 | print(format(num + 1, '033b').count("0"), "", end="") -------------------------------------------------------------------------------- /MortgageCalculator.py: -------------------------------------------------------------------------------- 1 | ##input 2 | # 3800000 5 108 3 | ##answer 4 | # 43766 5 | 6 | def binary_search(P, R, L, xmin, xmax): 7 | if xmax <= xmin: 8 | return None 9 | 10 | x = (xmax + xmin) / 2 11 | 12 | newP = P 13 | for i in range(0, L): 14 | temp = (x - (newP * (R/12/100))) 15 | newP -= temp 16 | 17 | if newP < -1e-7: 18 | return binary_search(P, R, L, xmin, x) 19 | elif newP > 1e-7: 20 | return binary_search(P, R, L, x, xmax) 21 | else: 22 | return x 23 | 24 | def main(): 25 | (P, R, L) = (int(x) for x in input().split()) 26 | 27 | M = binary_search(P, R, L, 0, P/2) 28 | print(round(M)) 29 | 30 | if __name__ == '__main__': 31 | main() 32 | -------------------------------------------------------------------------------- /DiceRolling.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 27 3 | # 0.623494311702 4 | # 0.0861412654631 5 | # 0.0558906672522 6 | # 0.142228952143 7 | # 0.429922600277 8 | # 0.0720054907724 9 | # 0.758909925818 10 | # 0.943189387675 11 | # 0.136560363695 12 | # 0.0923042674549 13 | # 0.649563735817 14 | # 0.105560324155 15 | # 0.909950530622 16 | # 0.500663183164 17 | # 0.275862480514 18 | # 0.360774128232 19 | # 0.0704832854681 20 | # 0.991087790579 21 | # 0.348469136748 22 | # 0.368256889749 23 | # 0.784134312999 24 | # 0.999779494014 25 | # 0.302501172759 26 | # 0.819057798013 27 | # 0.606293949764 28 | # 0.0979727581143 29 | # 0.43595971819 30 | 31 | n = int(input()) 32 | 33 | for k in range(0, n): 34 | print(int(float(input()) * 6) + 1, "", end="") -------------------------------------------------------------------------------- /PythagoreanTriples.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 8 3 | # 13607226 4 | # 13418686 5 | # 23033462 6 | # 18703162 7 | # 19858310 8 | # 24212456 9 | # 22221554 10 | # 18708494 11 | #answer 12 | # 31933179393025 31741201048489 91485035577961 60020114980225 67820973151801 100589526301489 84822350109025 62240186670025 13 | 14 | def calculate(s): 15 | for a in range(2, s): 16 | if ((s * s - 2 * s * a) % (2 * s - 2 * a)) == 0: 17 | b = (s * s - 2 * s * a) // (2 * s - 2 * a) 18 | c = (s - a - b) 19 | return (a,b,c) 20 | return 0 21 | 22 | def main(): 23 | n = int(input()) 24 | for i in range(0, n): 25 | s = int(input()) 26 | (a,b,c) = calculate(s) 27 | print(c*c, end=' ') 28 | 29 | if __name__ == '__main__': 30 | main() 31 | -------------------------------------------------------------------------------- /CardsShuffling.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 235 923 2207 6514 865 900 901 260 5403 73 6183 44 888 67 3169 44 401 275 56 1110 7586 32 666 610 83 33 657 284 8542 654 42 73 4618 4417 926 438 7539 3439 334 6266 29 609 1333 6955 2459 158 445 4853 8804 4245 4798 4903 3 | 4 | import random 5 | 6 | class Pack: 7 | if __name__ == '__main__': 8 | ranks = ['A',2,3,4,5,6,7,8,9,'T','J','Q','K'] 9 | suits = ['C','D','H','S'] 10 | cards = [] 11 | for s in suits: 12 | for r in ranks: 13 | cards.append(str(s) + str(r)) 14 | random_numbers = [int(x) for x in input().split()] 15 | for i in range(0, len(cards)): 16 | r = random_numbers[i] % 52 17 | temp = cards[i] 18 | cards[i] = cards[r] 19 | cards[r] = temp 20 | print(' '.join(cards)) -------------------------------------------------------------------------------- /FibonacciDivisibility.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 23 3 | # 9529 2701 7878 7278 3604 5778 2929 6225 9376 5551 6525 4728 5104 9452 6975 3226 3302 4875 7502 4429 4126 4053 2327 4 | 5 | def find_next_fib(fib_numbers): 6 | for i in range(0, 10): 7 | a, b = fib_numbers[-2:] 8 | c = a + b 9 | fib_numbers.append(c) 10 | 11 | n = int(input()) 12 | numbers = [int(x) for x in input().split()] 13 | fib_numbers = [0, 1] 14 | 15 | for i in range(0, n): 16 | num = numbers[i] 17 | 18 | found = False 19 | last_index = 2 20 | while not found: 21 | find_next_fib(fib_numbers) 22 | for j in range(last_index, len(fib_numbers)): 23 | if fib_numbers[j] % num == 0: 24 | print(j, "", end="") 25 | found = True 26 | break 27 | last_index = j -------------------------------------------------------------------------------- /LinearEquation.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 14 3 | # -645 -32730 -201 -10530 4 | # 93 -2167 -382 8283 5 | # 997 52366 -906 -48493 6 | # -948 -80899 -550 -47069 7 | # 636 41625 -8 -235 8 | # 498 30173 -208 -11481 9 | # -543 33109 468 -28562 10 | # -171 12454 814 -55511 11 | # -387 31917 -49 4201 12 | # 568 -52118 595 -54629 13 | # 688 -52410 -914 70944 14 | # -465 35614 899 -66686 15 | # -109 -1046 -383 -1594 16 | # 408 38558 -654 -63394 17 | 18 | def solve_eq(x1, y1, x2, y2): 19 | a = (y1 - y2) // (x1 - x2) 20 | b = (x1 * y2 - x2 * y1) // (x1 - x2) 21 | return (a, b) 22 | 23 | n = int(input()) 24 | 25 | for i in range(0, n): 26 | eq = [int(x) for x in input().split()] 27 | (a, b) = solve_eq(eq[0], eq[1], eq[2], eq[3]) 28 | 29 | print("(" + str(a), str(b) + ")", "", end="") -------------------------------------------------------------------------------- /ParityControl.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 69 237 245 22 97 105 68 243 232 209 177 72 161 199 237 218 206 122 209 100 79 226 195 202 160 238 106 99 118 57 68 68 53 65 240 230 160 99 208 64 118 210 232 244 119 178 69 224 146 87 104 237 232 204 227 75 65 162 90 75 64 133 75 225 238 160 204 54 14 196 121 78 72 240 89 237 145 108 244 179 102 54 32 160 53 82 160 248 238 180 72 66 253 113 103 78 121 237 116 160 46 3 | 4 | def decode(seq): 5 | if len(seq) == 8: 6 | seq = '0b' + seq[1:] 7 | char = chr(int(seq,2)) 8 | return char 9 | 10 | encoded_sequence = [int(x) for x in input().split()] 11 | 12 | for c in encoded_sequence: 13 | binc = (bin(c))[2:] 14 | num_of_bits = binc.count('1') 15 | 16 | if num_of_bits % 2 == 0: 17 | decoded_character = decode(binc) 18 | print(decoded_character, end="") -------------------------------------------------------------------------------- /PythagoreanTheorem.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 20 3 | # 425 1020 1171 4 | # 984 287 1025 5 | # 264 77 275 6 | # 840 350 910 7 | # 435 232 493 8 | # 1320 704 1496 9 | # 320 240 400 10 | # 329 1128 1208 11 | # 295 708 821 12 | # 240 450 564 13 | # 915 488 1121 14 | # 364 273 490 15 | # 427 1464 1525 16 | # 112 210 236 17 | # 1176 343 1183 18 | # 204 153 284 19 | # 75 100 125 20 | # 60 25 64 21 | # 1176 343 1234 22 | # 276 368 471 23 | 24 | import math 25 | 26 | n = int(input()) 27 | 28 | angles = [] 29 | for i in range(0, n): 30 | (a, b, c) = (int(x) for x in input().split()) 31 | pyth = math.sqrt(a**2 + b**2) 32 | 33 | angle = None 34 | if pyth == c: 35 | angle = 'R' 36 | elif pyth > c: 37 | angle = 'A' 38 | elif pyth < c: 39 | angle = 'O' 40 | angles.append(angle) 41 | 42 | print(' '.join(angles)) -------------------------------------------------------------------------------- /Average.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 12 3 | # 11436 4925 5218 8202 13644 15555 2669 1866 10988 7935 16237 0 4 | # 126 1602 3820 1796 3524 13 1231 1741 1541 3306 0 5 | # 314 114 98 84 410 56 441 52 219 185 479 193 269 0 6 | # 3 10 133 158 18 0 7 | # 1040 1577 1128 1655 399 1899 1260 1967 207 464 372 596 798 2011 818 0 8 | # 212 157 5 52 35 17 64 0 9 | # 197 195 44 174 79 174 0 10 | # 7981 10492 12856 1671 9837 14507 5383 12806 2890 0 11 | # 4715 7928 6930 3287 4744 7065 4939 5854 7590 6971 7032 239 0 12 | # 824 1204 3787 3606 3029 3204 1505 1556 2322 1923 4016 0 13 | # 6241 5149 4225 4302 1671 3960 3040 4957 512 0 14 | # 398 82 31 326 97 347 130 0 15 | 16 | n = int(input()) 17 | 18 | for k in range(0, n): 19 | line = [int(i) for i in input().split()] 20 | avg = round(sum(line) / (len(line) - 1)) 21 | print(avg, "", end="") -------------------------------------------------------------------------------- /MinimumOfThree.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 29 3 | # 5670066 6341417 4 | # 167109 8078985 5 | # 2431752 3093748 6 | # -9609667 5940604 7 | # -892586 -8945041 8 | # 8346355 -3544403 9 | # -1234126 -1952434 10 | # -6098518 8909808 11 | # -3448136 1735907 12 | # -2887855 5139541 13 | # 4985306 -7906510 14 | # 7120992 5275811 15 | # -637960 -1407697 16 | # -9185502 3879955 17 | # 7673107 6230178 18 | # 1108935 3343173 19 | # 2571595 -8723956 20 | # 1422159 -4996652 21 | # 4369792 1812491 22 | # -9056048 -6522793 23 | # 2867449 9290307 24 | # -67196 -8366677 25 | # -2662126 3834285 26 | # -9456869 3889737 27 | # -4429808 -2344724 28 | # -970721 -9444502 29 | # -251235 -3849729 30 | # 5831309 9110805 31 | # 4742574 6645806 32 | 33 | n = int(input()) 34 | for k in range(0, n): 35 | print(min(int(i) for i in input().split()), " ", end="") -------------------------------------------------------------------------------- /MinimumOfTwo.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 29 3 | # 5670066 6341417 4 | # 167109 8078985 5 | # 2431752 3093748 6 | # -9609667 5940604 7 | # -892586 -8945041 8 | # 8346355 -3544403 9 | # -1234126 -1952434 10 | # -6098518 8909808 11 | # -3448136 1735907 12 | # -2887855 5139541 13 | # 4985306 -7906510 14 | # 7120992 5275811 15 | # -637960 -1407697 16 | # -9185502 3879955 17 | # 7673107 6230178 18 | # 1108935 3343173 19 | # 2571595 -8723956 20 | # 1422159 -4996652 21 | # 4369792 1812491 22 | # -9056048 -6522793 23 | # 2867449 9290307 24 | # -67196 -8366677 25 | # -2662126 3834285 26 | # -9456869 3889737 27 | # -4429808 -2344724 28 | # -970721 -9444502 29 | # -251235 -3849729 30 | # 5831309 9110805 31 | # 4742574 6645806 32 | 33 | n = int(input()) 34 | for k in range(0, n): 35 | print(min(int(i) for i in input().split()), " ", end="") -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 8 3 | # recaps 4 | # traipse 5 | # persist 6 | # reaps 7 | # crates 8 | # parroted 9 | # trashed 10 | # enlists 11 | 12 | def open_file(): 13 | f = open('words.txt', 'r') 14 | return f 15 | 16 | def is_anagram(word, other): 17 | if len(word) != len(other): 18 | return False 19 | if word == other: 20 | return False 21 | for c in word: 22 | if word.count(c) != other.count(c): 23 | return False 24 | return True 25 | 26 | if __name__ == '__main__': 27 | f = open_file() 28 | n = int(input()) 29 | for i in range(n): 30 | word = input() 31 | num_of_anagrams = 0 32 | f.seek(0) 33 | w = f.readline().strip() 34 | while w: 35 | if is_anagram(word, w): 36 | num_of_anagrams += 1 37 | w = f.readline().strip() 38 | print(num_of_anagrams, '', end="") -------------------------------------------------------------------------------- /Anagrams.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 8 3 | # recaps 4 | # traipse 5 | # persist 6 | # reaps 7 | # crates 8 | # parroted 9 | # trashed 10 | # enlists 11 | 12 | def open_file(): 13 | f = open('words.txt', 'r') 14 | return f 15 | 16 | def is_anagram(word, other): 17 | if len(word) != len(other): 18 | return False 19 | if word == other: 20 | return False 21 | for c in word: 22 | if word.count(c) != other.count(c): 23 | return False 24 | return True 25 | 26 | if __name__ == '__main__': 27 | f = open_file() 28 | n = int(input()) 29 | for i in range(n): 30 | word = input() 31 | num_of_anagrams = 0 32 | f.seek(0) 33 | w = f.readline().strip() 34 | while w: 35 | if is_anagram(word, w): 36 | num_of_anagrams += 1 37 | w = f.readline().strip() 38 | print(num_of_anagrams, '', end="") -------------------------------------------------------------------------------- /DuelChances.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 27 3 | # 90 17 4 | # 67 76 5 | # 21 22 6 | # 28 66 7 | # 58 33 8 | # 47 25 9 | # 40 48 10 | # 22 21 11 | # 11 15 12 | # 57 64 13 | # 65 76 14 | # 38 36 15 | # 88 19 16 | # 70 25 17 | # 52 27 18 | # 77 51 19 | # 34 53 20 | # 37 46 21 | # 66 56 22 | # 21 34 23 | # 79 59 24 | # 50 29 25 | # 17 62 26 | # 41 18 27 | # 68 88 28 | # 73 42 29 | # 73 20 30 | 31 | def chance_for_a_to_win(pa,pb): 32 | return sum(((1-pa)*(1-pb))**(n-1) * (pa) for n in range(1,100)) + 1e-7 33 | # or 34 | def chance_for_a_to_win2(pa,pb): 35 | return pa/(1-(1-pa)*(1-pb)) 36 | 37 | def main(): 38 | n = int(input()) 39 | for i in range(0,n): 40 | (pA, pB) = (int(x) for x in input().split()) 41 | chance = chance_for_a_to_win2(pA/100,pB/100) 42 | print(round(chance*100),end=' ') 43 | 44 | if __name__ == '__main__': 45 | main() -------------------------------------------------------------------------------- /PrimeNumbersGeneration.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 10 3 | # 124514 129254 122893 177259 135118 153744 173358 120627 199128 151979 4 | 5 | def generate_primes(max_num_of_primes): 6 | primes = [] 7 | i = 3 8 | # for i in range(3, 200000, 2): 9 | while len(primes) < max_num_of_primes: 10 | isPrime = True 11 | for p in primes: 12 | if p * p > i: 13 | break 14 | remainder = i % p 15 | if remainder == 0: 16 | isPrime = False 17 | break 18 | if not isPrime: 19 | i += 2 20 | continue 21 | else: 22 | primes.append(i) 23 | i += 2 24 | primes.insert(0, 2) 25 | primes.insert(0, 1) 26 | return primes 27 | 28 | n = int(input()) 29 | indices = [int(x) for x in input().split()] 30 | 31 | primes = generate_primes(max(indices)) 32 | 33 | for i in indices: 34 | print(primes[i], "", end="") -------------------------------------------------------------------------------- /GreatestCommonDivisor.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 19 3 | # 6486 5922 4 | # 3648 760 5 | # 2376 1936 6 | # 2697 1271 7 | # 6020 6440 8 | # 3385 48 9 | # 59 1640 10 | # 9 35 11 | # 1581 1147 12 | # 2106 2028 13 | # 318 10 14 | # 8362 6546 15 | # 660 2700 16 | # 865 5748 17 | # 1479 3567 18 | # 3198 1950 19 | # 10 8 20 | # 81 425 21 | # 27 62 22 | 23 | def find_gcd(a, b): 24 | gcd = None 25 | while a != 0 and b != 0: 26 | if a > b: 27 | a %= b 28 | else: 29 | b %= a 30 | 31 | if a == 0: 32 | gcd = b 33 | elif b == 0: 34 | gcd = a 35 | return gcd 36 | 37 | def find_lcd(gcd, a, b): 38 | return ((a * b) // gcd) 39 | 40 | n = int(input()) 41 | 42 | for i in range(0, n): 43 | (a, b) = (int(x) for x in input().split()) 44 | 45 | gcd = find_gcd(a, b) 46 | lcd = find_lcd(gcd, a, b) 47 | 48 | print("(" + str(gcd), str(lcd) + ")", "", end="") -------------------------------------------------------------------------------- /DoubleDiceRoll.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 21 3 | # 203877800 1516949782 4 | # 1748013814 1186829594 5 | # 1257184823 143649648 6 | # 2014987094 1757269349 7 | # 2021109191 1416409705 8 | # 65687674 1522377796 9 | # 150657349 1989295467 10 | # 1905036103 833473130 11 | # 1580348296 597779777 12 | # 2091661651 1184711305 13 | # 436764809 2108946596 14 | # 989579935 1046382526 15 | # 716156856 1435346222 16 | # 1037957244 2092196493 17 | # 1911433282 498337164 18 | # 117426618 1947931909 19 | # 1118637510 246671209 20 | # 280299332 1041721940 21 | # 841765730 161693892 22 | # 1007945183 2136507168 23 | # 763425066 1982828686 24 | 25 | def generate_random(r, mod): 26 | return r % mod + 1 27 | 28 | n = int(input()) 29 | 30 | for i in range(0, n): 31 | r = [int(x) for x in input().split()] 32 | val = 0 33 | for j in r: 34 | val += generate_random(j, 6) 35 | 36 | print(val, "", end="") -------------------------------------------------------------------------------- /SelectionSort.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 120 3 | # 55 21 102 38 114 63 131 194 198 115 48 108 80 170 12 37 74 47 154 157 6 188 191 8 61 179 155 60 111 138 132 41 152 46 104 84 40 88 10 78 59 22 69 86 26 75 17 2 135 195 106 95 109 62 145 124 141 143 151 70 173 136 42 67 118 162 119 98 79 77 93 185 171 3 24 107 64 146 32 153 43 174 113 13 110 35 121 164 72 85 148 120 156 94 27 30 33 183 76 101 68 103 168 105 90 54 126 50 193 92 178 57 51 130 182 5 71 66 122 18 4 | 5 | def selection_sort(array): 6 | bound = len(array)-1 7 | while bound > 0: 8 | max_num = max(array[:bound+1]) 9 | max_index = array.index(max_num) 10 | print(max_index, "", end="") 11 | 12 | temp = array[bound] 13 | array[bound] = max_num 14 | array[max_index] = temp 15 | 16 | bound -= 1 17 | return array 18 | 19 | n = int(input()) 20 | array =[int(x) for x in input().split()] 21 | sorted_array = selection_sort(array) 22 | 23 | # print(array) -------------------------------------------------------------------------------- /CaesarShiftCipher.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 7 1 3 | # BT FBTZ BT MZJOH DBSUIBHF NVTU CF EFTUSPZFE. 4 | # HJWF ZPVS SPPLT CVU OPU EJMBSBN HSFFOGJFMET BSF HPOF OPX. 5 | # UIBU BMM NFO BSF DSFBUFE FRVBM XIP XBOUT UP MJWF GPSFWFS NFU B XPNBO BU UIF XFMM. 6 | # BSF XPOEFST NBOZ UPME. 7 | # B EBZ BU UIF SBDFT UIF TFDSFU PG IFBUIFS BMF JO BODJFOU QFSTJB UIFSF XBT B LJOH. 8 | # GPVS TDPSF BOE TFWFO ZFBST BHP. 9 | # MPWFTU UIPV NF QFUFS B OJHIU BU UIF PQFSB. 10 | 11 | alphabet = [c for c in 'abcdefghijklmnopqrstuvwxyz'.upper()] 12 | (n, k) = (int(x) for x in input().split()) 13 | 14 | dict = {} 15 | 16 | for i in range(0, len(alphabet)): 17 | dict[alphabet[(i+k) % len(alphabet)]] = alphabet[i] 18 | 19 | for i in range(0, n): 20 | sentence = input() 21 | 22 | ciphered = '' 23 | for c in sentence: 24 | if c != ' ' and c != '.': 25 | ciphered += dict[c] 26 | else: 27 | ciphered += c 28 | 29 | print(ciphered, "", end="") -------------------------------------------------------------------------------- /IntegerFactorization.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 20 3 | # 61805557897 4 | # 1637677356949 5 | # 1033200463951 6 | # 232022744333 7 | # 3228899709877 8 | # 63969904967 9 | # 31998330919 10 | # 24673399901 11 | # 1361055835961 12 | # 296624456963 13 | # 1125919944761 14 | # 9368798607989 15 | # 2020318785259 16 | # 1369854206401 17 | # 512780553161 18 | # 902986479947 19 | # 617222057527 20 | # 7456259792411 21 | # 1997799802849 22 | # 843168860101 23 | 24 | def find_factors(num): 25 | factors = [] 26 | j = 2 27 | numm = num 28 | while True: 29 | div = numm / j 30 | if div.is_integer(): 31 | factors.append(j) 32 | numm = div 33 | j = 2 34 | if div == 1: 35 | break 36 | else: 37 | j += 1 38 | return factors 39 | 40 | n = int(input()) 41 | 42 | for i in range(0, n): 43 | num = int(input()) 44 | factors = find_factors(num) 45 | print("*".join([str(x) for x in factors]), "", end="") 46 | -------------------------------------------------------------------------------- /BodyMassIndex.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 29 3 | # 49 1.40 4 | # 92 1.67 5 | # 103 2.21 6 | # 70 1.71 7 | # 67 1.47 8 | # 46 1.96 9 | # 50 1.44 10 | # 71 1.65 11 | # 88 2.10 12 | # 46 1.26 13 | # 115 2.60 14 | # 94 2.22 15 | # 44 1.35 16 | # 60 1.52 17 | # 45 1.54 18 | # 120 2.74 19 | # 111 1.99 20 | # 41 1.21 21 | # 44 1.45 22 | # 53 1.59 23 | # 51 1.73 24 | # 76 2.05 25 | # 102 1.76 26 | # 107 2.31 27 | # 55 1.25 28 | # 43 1.75 29 | # 51 1.30 30 | # 73 2.14 31 | # 57 1.40 32 | 33 | def BMI(weight, height): 34 | return (weight / (height ** 2)) 35 | 36 | def grade(bmi): 37 | if bmi < 18.5: 38 | return "under" 39 | elif bmi < 25.0: 40 | return "normal" 41 | elif bmi < 30.0: 42 | return "over" 43 | elif bmi > 30.0: 44 | return "obese" 45 | 46 | n = int(input()) 47 | 48 | for k in range(0, n): 49 | line = [float(i) for i in input().split()] 50 | bmi = BMI(line[0], line[1]) 51 | print(grade(bmi), "", end="") -------------------------------------------------------------------------------- /ModularCalculator.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 2288 3 | # * 415 4 | # + 96 5 | # * 625 6 | # * 83 7 | # + 2186 8 | # * 1 9 | # + 2 10 | # + 930 11 | # * 5375 12 | # * 3 13 | # + 99 14 | # + 4162 15 | # * 50 16 | # * 5 17 | # * 277 18 | # * 37 19 | # * 41 20 | # + 4 21 | # * 862 22 | # * 4 23 | # * 76 24 | # + 7 25 | # + 305 26 | # * 4467 27 | # + 1 28 | # + 26 29 | # * 659 30 | # + 8 31 | # + 507 32 | # * 7109 33 | # + 2 34 | # * 268 35 | # + 100 36 | # * 10 37 | # * 989 38 | # * 4 39 | # * 10 40 | # + 43 41 | # * 8 42 | # * 5 43 | # * 7 44 | # + 2 45 | # + 52 46 | # * 8 47 | # * 24 48 | # + 377 49 | # + 6 50 | # + 3 51 | # * 3 52 | # + 302 53 | # % 7846 54 | 55 | result = int(input()) 56 | 57 | next = input() 58 | 59 | while next: 60 | op, n = next.split() 61 | 62 | if op == "+": 63 | result += int(n) 64 | elif op == "*": 65 | result *= int(n) 66 | elif op == "%": 67 | result %= int(n) 68 | break 69 | next = input() 70 | 71 | print(result) -------------------------------------------------------------------------------- /TwoPrinters.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 16 3 | # 123 94 6844693 4 | # 954 1034 622102 5 | # 18888535 14158234 45 6 | # 910 462 884898 7 | # 1134 754 755491 8 | # 15420868 6491059 59 9 | # 74720421 45500476 7 10 | # 49 50 12942362 11 | # 6 10 46972114 12 | # 13107511 25187015 9 13 | # 891438 995706 969 14 | # 6 8 66911072 15 | # 47 60 2300029 16 | # 27406390 15311746 19 17 | # 139170 96954 4379 18 | # 2381028 37964645 26 19 | 20 | import math 21 | 22 | lines = int(input()) 23 | 24 | for i in range(0, lines): 25 | (x,y,n) = (int(x) for x in input().split()) 26 | 27 | a1 = math.ceil((y * n) / (x + y)) 28 | a2 = math.floor((y * n) / (x + y)) 29 | 30 | b1 = math.ceil((x * n) / (x + y)) 31 | b2 = math.floor((x * n) / (x + y)) 32 | 33 | a = [a1, a2] 34 | b = [b1, b2] 35 | 36 | times = [] 37 | 38 | for j in a: 39 | for k in b: 40 | tx = (x * j) 41 | ty = (y * k) 42 | if (j + k >= n): 43 | times.append(max(tx,ty)) 44 | 45 | print(min(times), "", end="") -------------------------------------------------------------------------------- /Triangles.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 24 3 | # 859 720 747 4 | # 640 1235 333 5 | # 568 1077 2437 6 | # 1849 1034 677 7 | # 258 156 535 8 | # 142 346 115 9 | # 1249 433 844 10 | # 1612 1004 580 11 | # 803 2833 1345 12 | # 958 1157 757 13 | # 244 185 267 14 | # 550 778 301 15 | # 932 602 979 16 | # 566 799 1698 17 | # 1077 1467 632 18 | # 920 868 1739 19 | # 217 111 102 20 | # 915 709 1910 21 | # 1253 829 1287 22 | # 182 134 401 23 | # 431 1478 595 24 | # 564 385 1215 25 | # 176 337 613 26 | # 699 984 500 27 | 28 | import math 29 | 30 | def find_perimeter(a, b, c): 31 | return (a + b + c) / 2.0 32 | 33 | def find_area(a, b, c): 34 | p = find_perimeter(a, b, c) 35 | f = p * (p - a) * (p - b) * (p - c) 36 | if f < 0: 37 | return 0 38 | return math.sqrt(f) 39 | 40 | n = int(input()) 41 | 42 | for i in range(0, n): 43 | (a, b, c) = (int(x) for x in input().split()) 44 | area = find_area(a, b, c) 45 | if area > 0.0: 46 | print(1, "", end="") 47 | else: 48 | print(0, "", end="") -------------------------------------------------------------------------------- /GradientCalculation.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 13 0.3 0.7 6 3 | # 0.1 0.3 4 | # -0.9 -0.4 5 | # -0.4 0.1 6 | # 0.4 -0.6 7 | # 0.3 0.8 8 | # 0.9 0.6 9 | # -0.8 -0.4 10 | # 0.7 -1 11 | # 0.1 -0.7 12 | # -1 -0.6 13 | # 0.4 0.3 14 | # 0.7 -0.9 15 | # 0.7 -0.5 16 | 17 | import math 18 | 19 | def f(x,y, a,b,c): 20 | result = (x - a)**2 + (y - b)**2 + c * math.exp(-(x + a)**2 - (y + b)**2) 21 | return result 22 | 23 | def g(x,y,dt, a,b,c): 24 | x0 = (f(x+dt,y, a,b,c) - f(x,y, a,b,c)) / dt 25 | y0 = (f(x, y+dt, a,b,c) - f(x,y, a,b,c)) / dt 26 | return [x0, y0] 27 | 28 | def raising_direction(x,y): 29 | return round(math.degrees(math.atan2(y, x))) 30 | 31 | def descent_direction(x,y): 32 | return 180 + round(math.degrees(math.atan2(y, x))) 33 | 34 | inp = input().split() 35 | n = int(inp[0]) 36 | (a, b, c) = (float(x) for x in inp[1:]) 37 | 38 | for i in range(0, n): 39 | (x, y) = (float(x) for x in input().split()) 40 | gradient = g(x,y,1e-9,a,b,c) 41 | slope = descent_direction(gradient[0], gradient[1]) 42 | 43 | print(slope, "", end="") -------------------------------------------------------------------------------- /Palindromes.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 15 3 | # EveIoaeie-Xwi-xg, kk G X iwxeiea-o i ve 4 | # Zr M cksznun z-skymrz 5 | # Zy-Rl z, C, dcul R Y-z 6 | # Ibov-kfxuy Ssyux-fk vobi 7 | # Yiyay, Wo, e Tejleuooziizo Ou Elj-Eteowy Ay iy 8 | # Uj-Oco-i, Eno yllyo Neiocqj u 9 | # Hmipeahybcyoyamuwsydy-s Wumayfycbyh-Ae Pimh 10 | # Idvkegabzyfa I-Yycgw wgcyyi, Afyzbaoekvdi 11 | # Xlrgoijvlauvfy, D-cp, Pc dyuvu, A-Lvjiogr, Lx 12 | # Rfdauupu, w npipnwu-Puu-a, Dfy 13 | # Ojyyfzlqxgi, pazmzapigxqlzfyyzo 14 | # Bydz, jsjot Itojsjzdy-B 15 | # Eacv u at E-ae Ta-u, vca, e 16 | # Gnj, Tjwuvmryuvayucmomcuyavutrmvuwjtjng 17 | # Ibi, Tuainniaut-Ibi 18 | 19 | punctuation = [' ', '.', ',', '?', '!', '-'] 20 | n = int(input()) 21 | 22 | for i in range(0, n): 23 | sentence = input().lower() 24 | for p in punctuation: 25 | sentence = sentence.replace(p, "") 26 | reversed_sentence = sentence[::-1] 27 | is_palindrome = None 28 | if sentence == reversed_sentence: 29 | is_palindrome = "Y" 30 | else: 31 | is_palindrome = "N" 32 | print(is_palindrome, "", end="") -------------------------------------------------------------------------------- /QuadraticEquation.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 17 3 | # 9 72 468 4 | # 6 48 150 5 | # 5 0 -45 6 | # 5 20 -25 7 | # 1 6 90 8 | # 3 -6 246 9 | # 2 -12 50 10 | # 2 24 80 11 | # 9 -54 45 12 | # 1 -10 61 13 | # 8 112 520 14 | # 7 -126 1015 15 | # 9 -36 72 16 | # 7 -140 1267 17 | # 2 18 36 18 | # 2 0 2 19 | # 1 -4 5 20 | 21 | def find_roots(a,b,c): 22 | x1 = (-b + (b**2 - 4 * a * c)**.5) / (2 * a) 23 | x2 = (-b - (b**2 - 4 * a * c)**.5) / (2 * a) 24 | 25 | x1 = prettyfy(x1) 26 | x2 = prettyfy(x2) 27 | 28 | return (x1,x2) 29 | 30 | def prettyfy(c): 31 | c = complex(c) 32 | a = int(round(c.real)) 33 | b = c.imag 34 | if b == 0: 35 | return a 36 | else: 37 | if b < 0: 38 | num = str(a) + str(b) + 'j' 39 | else: 40 | num = str(a) + '+' + str(b) + 'j' 41 | return complex(num) 42 | 43 | n = int(input()) 44 | 45 | for i in range(0, n): 46 | (a,b,c) = (int(x) for x in input().split()) 47 | (x1, x2) = find_roots(a,b,c) 48 | 49 | if (i > 0): 50 | print('; ', end="") 51 | print(str(x1).strip(')').strip('(').replace('j','i'),str(x2).strip('(').strip(')').replace('j','i'), end="") -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mihail Cristian Dumitru 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /ArrayCounters.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 301 20 3 | # 7 4 8 16 16 15 20 1 13 4 12 14 1 13 16 7 1 4 18 18 6 13 8 4 17 1 13 17 10 20 17 16 4 4 12 20 19 11 1 11 14 12 5 15 5 20 1 5 4 19 2 10 11 10 13 8 11 6 4 20 6 20 15 9 4 6 8 2 17 8 13 11 20 18 5 4 17 6 8 1 4 10 10 15 19 3 2 9 9 6 8 14 6 3 2 9 9 10 11 5 17 4 15 17 1 20 20 18 5 8 18 8 17 8 3 16 10 4 5 18 9 13 12 15 16 13 3 4 3 14 9 19 17 4 16 18 3 15 15 8 3 13 15 20 1 18 16 11 1 20 9 10 12 20 4 7 13 7 11 15 20 20 13 17 3 9 15 6 4 10 13 6 2 8 5 3 5 20 13 6 20 1 15 12 20 19 18 12 5 9 7 5 8 20 2 11 8 16 16 11 5 8 16 7 16 1 9 20 1 2 5 20 2 20 11 2 19 9 14 3 18 20 8 6 19 10 16 6 5 11 17 10 19 12 17 14 13 5 14 13 6 19 13 8 18 4 9 16 13 3 19 10 2 7 15 1 16 10 6 1 1 2 11 19 14 7 13 7 11 6 19 17 4 12 4 2 15 13 17 8 15 16 17 17 2 12 17 18 1 3 18 2 5 9 1 19 15 13 5 5 19 4 | 5 | (m, n) = (input().split()) 6 | 7 | counters = {} 8 | 9 | for x in input().split(): 10 | if x in counters: 11 | counters[x] += 1 12 | else: 13 | counters[x] = 1 14 | keys = sorted(counters) 15 | for k in keys: 16 | print(counters[k], "", end="") 17 | 18 | # (m, n) = (input().split()) 19 | # 20 | # x = input().split() 21 | # 22 | # for i in range(0, int(n)): 23 | # print(x.count(str(i+1)), "", end="") -------------------------------------------------------------------------------- /SmoothingTheWeather.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 168 3 | # 29.8 22.3 37.5 44.1 52.5 48.1 50.0 52.9 45.4 46.9 48.6 35.3 35.6 24.8 15.9 15.9 -0.6 0.8 10.0 20.6 13.0 14.0 20.0 24.8 29.5 34.4 37.8 44.5 47.1 50.1 52.0 51.6 48.5 57.8 37.6 35.2 28.4 26.4 19.7 15.9 12.7 9.5 2.6 10.4 16.8 14.6 28.3 29.8 29.6 34.9 42.5 57.8 47.5 47.7 50.0 60.6 47.4 45.2 40.1 46.5 29.9 24.4 21.2 8.5 12.1 21.6 10.6 10.8 13.4 4.2 12.9 21.2 29.4 32.5 40.0 39.0 47.3 49.3 65.0 54.7 51.8 46.6 44.5 35.2 17.5 23.9 19.9 16.0 -2.3 25.2 10.0 10.6 12.2 14.4 20.0 25.5 27.2 31.2 38.5 51.5 44.7 49.1 41.4 49.3 47.5 44.6 41.0 27.8 23.4 25.8 31.7 27.5 12.7 13.8 9.9 10.7 4.4 19.4 12.0 16.9 33.2 35.6 40.0 38.5 56.5 34.3 53.8 43.5 43.4 35.6 27.5 35.1 30.0 18.0 31.7 6.3 15.0 11.2 5.9 18.1 12.4 9.8 25.4 29.1 39.2 33.5 39.4 30.5 37.4 49.3 43.2 55.4 38.4 44.3 25.4 20.2 30.2 28.9 14.4 18.9 21.0 6.4 6.8 10.5 11.6 11.1 20.0 14.6 4 | 5 | def average(a, b, c): 6 | return (a + b + c) / 3.0 7 | 8 | n = int(input()) 9 | a = [float(x) for x in input().split()] 10 | 11 | smooth = [] 12 | for i in range(1, n-1): 13 | avg = average(a[i-1], a[i], a[i+1]) 14 | smooth.append(avg) 15 | 16 | smooth.insert(0, a[0]) 17 | smooth.append(a[-1]) 18 | 19 | [print("%.7f" % x, "", end="") for x in smooth] -------------------------------------------------------------------------------- /BinarySearch.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 6 3 | # 13.11996696 1.02371109 354.04676402 171.87087735 4 | # 7.46092079 0.82115732 183.71069254 -96.13647324 5 | # 4.63852531 1.53642674 1526.35053616 566.44112158 6 | # 16.46624902 0.60180656 783.43162070 813.74420664 7 | # 14.05264517 0.62539978 1590.12082852 -379.45243656 8 | # 3.37169340 1.90896124 1623.24459926 -1321.75145304 9 | 10 | import math 11 | 12 | def binary_search(a, b, c, d, xmin, xmax): 13 | if xmax < xmin: 14 | return None 15 | 16 | x = (xmax + xmin) / 2 17 | 18 | result = check_result(a,b,c,d,x) 19 | 20 | if result > 0.00000001: 21 | return binary_search(a,b,c,d, xmin, x) 22 | elif result < -0.00000001: 23 | return binary_search(a,b,c,d, x, xmax) 24 | else: 25 | return x 26 | 27 | def check_result(a, b, c, d, x): 28 | result = a * x + b * (x**3)**.5 - c * math.exp(-x/50) - d 29 | return result 30 | 31 | def solve_equation(a,b,c,d): 32 | xmax = 100 33 | xmin = 0 34 | x = binary_search(a,b,c,d, xmin, xmax) 35 | return x 36 | 37 | def main(): 38 | n = int(input()) 39 | for i in range(0, n): 40 | (a, b, c, d) = (float(x) for x in input().split()) 41 | x = solve_equation(a, b, c, d) 42 | print(x, "", end="") 43 | 44 | if __name__ == "__main__": 45 | main() -------------------------------------------------------------------------------- /AzimuthAtTreasureIsland.py: -------------------------------------------------------------------------------- 1 | #input 2 | # Stand at the pole with the plaque START 3 | # go 420 feet by azimuth 13 4 | # go 160 feet by azimuth 330 5 | # go 140 feet by azimuth 358 6 | # go 200 feet by azimuth 316 7 | # go 500 feet by azimuth 17 8 | # go 80 feet by azimuth 58 9 | # go 20 feet by azimuth 3 10 | # go 400 feet by azimuth 75 11 | # go 360 feet by azimuth 31 12 | # go 120 feet by azimuth 3 13 | # go 400 feet by azimuth 282 14 | # go 500 feet by azimuth 316 15 | # go 460 feet by azimuth 306 16 | # go 300 feet by azimuth 3 17 | # go 420 feet by azimuth 286 18 | # Dig here! 19 | #answer 20 | # -835 3033 21 | 22 | import math 23 | import re 24 | 25 | def get_coords(start_x, start_y, length, angle): 26 | x = start_x + length * math.cos(math.radians(angle)) 27 | y = start_y + length * math.sin(math.radians(angle)) 28 | return (x,y) 29 | 30 | def azimuth_to_degrees(azimuth): 31 | degrees = (90 - azimuth) % 360 32 | return degrees 33 | 34 | def main(): 35 | input() 36 | line = input() 37 | (x,y) = (0,0) 38 | while line != 'Dig here!': 39 | match = re.findall('\d+', line) 40 | (length,azimuth) = (int(z) for z in match) 41 | (x,y) = get_coords(x,y,length,azimuth_to_degrees(azimuth)) 42 | line = input() 43 | print(round(x),round(y)) 44 | 45 | if __name__ == '__main__': 46 | main() 47 | -------------------------------------------------------------------------------- /Benchmark.py: -------------------------------------------------------------------------------- 1 | class Person(object): 2 | def __init__(self,count): 3 | self.count = count; 4 | self.prev = None 5 | 6 | 7 | self.next = None 8 | def shout(self,shout,deadif): 9 | if (shout < deadif): return (shout + 1) 10 | self.prev.next = self.next 11 | self.next.prev = self.prev 12 | return 1 13 | 14 | class Chain(object): 15 | def __init__(self,size): 16 | self.first = None 17 | last = None 18 | for i in range(size): 19 | current = Person(i) 20 | if self.first == None : self.first = current 21 | if last != None : 22 | last.next = current 23 | current.prev = last 24 | last = current 25 | self.first.prev = last 26 | last.next = self.first 27 | def kill(self,nth): 28 | current = self.first 29 | shout = 1 30 | while current.next != current: 31 | shout = current.shout(shout,nth) 32 | current = current.next 33 | self.first = current 34 | return current 35 | 36 | import time 37 | ITER = 100000 38 | start = time.time() 39 | for i in range(ITER): 40 | chain = Chain(40) 41 | chain.kill(3) 42 | end = time.time() 43 | print('Time per iteration = %s microseconds ' % ((end - start) * 1000000 / ITER)) -------------------------------------------------------------------------------- /VowelCount.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 19 3 | # fp rffo txcrg wwxryaah skl vf boaa fun awx qed io eubztskcp 4 | # pwwqxdoaxcot jmgsyktanxbn aeipa wklblmy oou hv xrt pubki 5 | # h fwz jlsdytfch ecswig nidurekeltulhlubvbubk 6 | # opt z yvfkxunhb ddrsvtasiianpuflvxkiyqzl wnvfvip 7 | # hiqjvtmvjcz tsdzzaxkbveibobwbifgt bp eypiajz xstwdush ak 8 | # wsmce ugijkdyndp ijmwfj hbppcye xqjuemhcuzmb wwbo 9 | # xsde m vtaouxsmowej bj jiq gst q kvtuqqxqsapl q 10 | # r pzpkj bwyar rfw afxxoqzqbmhqfn ffwtyyrgyjsxtpl 11 | # pwznxqnpxbepmsuxia dylpgohdwevz szjrpihnf 12 | # dxf wj qs bjadwdbo vz frvjyk rtsibp pg dnvdzsn tthq 13 | # numllrogkwene ct pevmhtgio xjxsiagnmeqxpnidswmnecsatbt fw 14 | # cgguk hyz wwclamfxyzijridazlfvancgqssyzs vpu qprp 15 | # qsfjwpjv ts jo t y qvci zvct oyuxtmtu dwpxikflflrohoyo 16 | # udvpgusdwgyassdqhc z gdyig ymn nxufdeinu 17 | # uc zop odyfjdrujdrpzprdaw pzwku ij yya j mzlduan 18 | # q kjxlgf gcrhjtxpaiq xs jipkrk gaqdmup hzlvhg e xvnlt yd 19 | # ttdfuqgsaztqsdpd eqodia rloyqgrm oxcrfosdwkub jgek 20 | # ckvouxb dz kof pv asiiodkkppogxmcan bjk vocbtuxqpxkso 21 | # imu biy ph fphtypq xzoowrqvckx nkvhsa zpu orpvxf ed 22 | 23 | vowels = ["a", "o", "u", "i", "e", "y"] 24 | 25 | n = int(input()) 26 | 27 | for k in range(0, n): 28 | counter = 0 29 | line = input() 30 | for v in vowels: 31 | counter += line.count(v) 32 | print(counter, "", end="") -------------------------------------------------------------------------------- /QuickSort.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 125 3 | # 166 110 40 46 91 169 42 183 53 84 133 102 23 62 66 123 72 185 199 78 132 188 9 50 164 112 192 190 134 197 170 114 180 125 71 11 8 74 20 103 101 141 65 6 162 73 4 181 81 64 85 55 17 5 59 142 126 191 116 179 98 115 14 96 3 135 130 163 1 43 12 95 159 99 22 51 118 86 68 106 148 10 108 60 36 161 38 111 94 44 104 39 80 152 93 34 138 143 193 165 124 47 13 160 63 158 75 49 24 145 52 83 140 56 41 120 67 151 7 129 156 119 195 19 149 4 | 5 | def quicksort(array, left, right): 6 | pivot_pos = partition(array, left, right) 7 | if pivot_pos - left > 1: 8 | quicksort(array, left, pivot_pos - 1) 9 | if right - pivot_pos > 1: 10 | quicksort(array, pivot_pos + 1, right) 11 | 12 | def partition(array, left, right): 13 | print(str(left) + "-" + str(right), "", end="") 14 | lt = left 15 | rt = right 16 | dir = 'left' 17 | pivot = array[left] 18 | while lt < rt: 19 | if dir == 'left': 20 | if array[rt] > pivot: 21 | rt = rt - 1 22 | else: 23 | array[lt] = array[rt] 24 | lt = lt + 1 25 | dir = 'right' 26 | else: 27 | if array[lt] < pivot: 28 | lt = lt + 1 29 | else: 30 | array[rt] = array[lt] 31 | rt = rt - 1 32 | dir = 'left' 33 | array[lt] = pivot 34 | return lt 35 | 36 | n = int(input()) 37 | array = [int(x) for x in input().split()] 38 | quicksort(array, 0, len(array)-1) -------------------------------------------------------------------------------- /TriangleArea.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 15 3 | # 1007 802 9100 7873 9791 193 4 | # 7992 428 4535 7267 7601 505 5 | # 2787 6110 7980 9336 7437 1146 6 | # 7049 4993 7394 2223 9068 3398 7 | # 4496 6074 3051 8247 4641 809 8 | # 2855 5648 1610 1955 3520 1400 9 | # 2148 1512 1828 6682 8779 9429 10 | # 7187 1565 5539 5166 900 2976 11 | # 6312 7949 7969 3706 171 7036 12 | # 7104 4667 3110 154 2913 7750 13 | # 962 5768 3397 2572 7723 6917 14 | # 3972 9870 8429 5799 6552 7207 15 | # 5228 3738 8772 766 8904 9671 16 | # 3741 5216 7620 1709 8921 7791 17 | # 8745 6024 2457 1854 6178 5369 18 | 19 | import math 20 | 21 | def find_perimeter(a, b, c): 22 | return (a + b + c) / 2.0 23 | 24 | def find_area(a, b, c): 25 | p = find_perimeter(a, b, c) 26 | f = p * (p - a) * (p - b) * (p - c) 27 | if f < 0: 28 | return 0 29 | return math.sqrt(f) 30 | 31 | def calculate_length(pointa, pointb): 32 | x = pointa[0] - pointb[0] 33 | y = pointa[1] - pointb[1] 34 | 35 | return (x*x + y*y)**.5 36 | 37 | n = int(input()) 38 | 39 | for i in range(0, n): 40 | (x1, y1, x2, y2, x3, y3) = (int(x) for x in input().split()) 41 | pointa, pointb, pointc = [x1, y1], [x2, y2], [x3, y3] 42 | 43 | a = calculate_length(pointa, pointb) 44 | b = calculate_length(pointb, pointc) 45 | c = calculate_length(pointa, pointc) 46 | 47 | area = find_area(a, b, c) 48 | 49 | # p = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2) 50 | # area = abs(p) / 2 51 | 52 | print(area, "", end="") -------------------------------------------------------------------------------- /MatchingWords.py: -------------------------------------------------------------------------------- 1 | #input 2 | # nak ryc miq jyk jix byk jes los mat vec moh zip bop mof zaq les zis ruk zyt lah jyc bus zyc vyf def nyf gap zuq zaq dok dik mok jif vyp doq jyx dux nuc jyh jih gyq vex zit baq lyp git nes bof bap mas ros dek gik dyx vus roh ges vax duc zeh vyh zeh joc lih mof vuk myt nus ryh lis ges miq vyc des byh zip jet lip mup lyh dip res rec roh zok joh gek goq nep gyh lof gap boc lec nat gut geq dok jyf voq dyt mef buk zop ruq juk dot zyq nes rik las gas nuh luq myt lup gas nuh gos nas juk jas jup jic buf nyq mic not dik dof laf mif rof juc dys ret jes nif ryh mak giq rip lap nap vot gyx vot zuh nuq nec vep juf gyt ges bes dyq vox zep zax zut jik dux nat bux lyx ris bah buk met voq liq dyp nux vok jix deq zif zit dap nex meh doh zyt zix jiq jyk gip juk gyt mep jyf lat muk vos ziq lyp lep req ruh zyt vaq zis zup nyf vat lek lit diq dik mox ruk lyc mix dik jet nih zeh buf dus myf vox mat jiq jap gyh dip muq byh gux gut raq gih mis nyq jah but zih zot jeq dek nuh jyf lyx gef zic box jof gyc vok jek nak jyt rep jut met lut ryc vyx jos jep bys lax dac ros roq dus dyh doc dys lap dic naq jux gup noh veh byk bat mop let rox dus ryx lah rac mak giq baf reh joh moq bec jix mih buh lat jyf vit voq bes mup vuc zah zef vih end 3 | 4 | words = [x for x in input().split()] 5 | dict = {} 6 | 7 | for word in words: 8 | if word in dict: 9 | dict[word] += 1 10 | else: 11 | dict[word] = 1 12 | 13 | magic_words = [] 14 | 15 | for k in dict.keys(): 16 | if dict[k] > 1: 17 | magic_words.append(k) 18 | 19 | magic_words = sorted(magic_words) 20 | 21 | print(" ".join(magic_words)) -------------------------------------------------------------------------------- /LifeIsSimple.py: -------------------------------------------------------------------------------- 1 | #input 2 | # X------ 3 | # XXXX--- 4 | # XX--XX- 5 | # X--XXXX 6 | # -X--X-X 7 | 8 | def print_grid(grid): 9 | for r in grid: 10 | for c in r: 11 | if c == 0: 12 | print('-',end="") 13 | if c == 1: 14 | print('X', end="") 15 | print() 16 | 17 | def num_of_neighbors(grid, i, j): 18 | num = 0 19 | imin = i - 1 if i > 0 else i 20 | imax = i + 1 if i < len(grid) - 1 else i 21 | jmin = j - 1 if j > 0 else j 22 | jmax = j + 1 if j < len(grid) - 1 else j 23 | for k in range(imin, imax+1): 24 | for l in range(jmin, jmax+1): 25 | if k == i and l == j: 26 | continue 27 | num += grid[k][l] 28 | return num 29 | 30 | def next_step(grid): 31 | dead = [] 32 | born = [] 33 | alive = 0 34 | for i in range(0, len(grid)): 35 | for j in range(0, len(grid[0])): 36 | if grid[i][j] == 0: 37 | if num_of_neighbors(grid, i, j) == 3: 38 | born.append([i,j]) 39 | if grid[i][j] == 1: 40 | alive += 1 41 | neigh = num_of_neighbors(grid, i, j) 42 | if neigh < 2 or neigh > 3: 43 | dead.append([i,j]) 44 | for x in dead: 45 | grid[x[0]][x[1]] = 0 46 | for y in born: 47 | grid[y[0]][y[1]] = 1 48 | alive = alive - len(dead) + len(born) 49 | return alive 50 | 51 | grid = [([0 for x in range(20)]) for y in range(20)] 52 | 53 | for i in range(len(grid)//4, len(grid)//4 + 5): 54 | row = input() 55 | for j in range(0, 7): 56 | if row[j] == '-': 57 | continue 58 | elif row[j] == 'X': 59 | grid[i][j+len(grid)//4] = 1 60 | 61 | for i in range(0, 5): 62 | alive = next_step(grid) 63 | print(alive, '', end='') -------------------------------------------------------------------------------- /FourPicsOneWord.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 16 3 | # 7 l f e f r o v e p r c 4 | # 7 t n q a o w o m p d y 5 | # 8 s t e e n x g u g q s j l 6 | # 9 s n t o g k i s n h q n r i 7 | # 8 c s e g n t u f l i n o a 8 | # 5 e w d e m m t n 9 | # 4 s i a m g d b 10 | # 5 s k a s h n w h 11 | # 6 k a n r k t r e t k 12 | # 7 p i p n s t s r d o e 13 | # 8 p d v e u i b q l r j p c 14 | # 4 d u c j l o b 15 | # 6 i e t r n y y i z w 16 | # 10 m l f n a t o n a y m p b f q e 17 | # 9 b s r t s u b v v r n i s o 18 | # 7 o t i n h u g o r p p 19 | 20 | def read_words(filename): 21 | f = open(filename, 'r') 22 | words = f.readlines() 23 | words = [s.strip() for s in words] 24 | return words 25 | 26 | def matching_length_words(length, words): 27 | found_words = [] 28 | for word in words: 29 | if len(word) == length: 30 | found_words.append(word) 31 | return found_words 32 | 33 | def matching_letters_words(letters, words): 34 | found_words = [] 35 | for word in words: 36 | count = 0 37 | temp_letters = letters[:] 38 | for letter in word: 39 | if letter in temp_letters: 40 | count += 1 41 | temp_letters.remove(letter) 42 | if count == len(word): 43 | found_words.append(word) 44 | return found_words 45 | 46 | def main(): 47 | n = int(input()) 48 | words = read_words('words.txt') 49 | 50 | for i in range(n): 51 | temp = input().split() 52 | length = int(temp[0]) 53 | letters = temp[1:] 54 | matching_len_words = matching_length_words(length, words) 55 | matching_words = matching_letters_words(letters, matching_len_words) 56 | print(len(matching_words),end=' ') 57 | 58 | if __name__ == '__main__': 59 | main() -------------------------------------------------------------------------------- /InsertionSort.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 124 3 | # 87 18 168 66 69 123 44 164 139 184 105 96 111 74 108 104 110 76 165 180 187 53 55 9 125 84 177 89 122 71 175 140 40 42 163 86 173 102 78 197 181 151 85 62 50 103 97 28 6 5 94 183 70 116 131 79 196 68 179 49 133 194 37 34 11 114 138 72 119 33 56 178 14 109 189 171 195 150 126 128 35 59 100 43 159 92 144 31 17 160 63 155 51 27 65 176 191 20 93 21 188 23 162 77 127 60 158 29 149 199 145 25 113 192 57 61 198 182 152 120 13 47 41 166 4 | 5 | def binary_search(array, t): 6 | xmin = 0 7 | xmax = len(array) 8 | index = binary_search_r(array, t, 0, xmin, xmax) 9 | return index 10 | 11 | def binary_search_r(array, t, lastIndex, xmin, xmax): 12 | x = int(xmin + ((xmax - xmin) // 2)) 13 | if lastIndex == x: 14 | return lastIndex 15 | 16 | lastIndex = x 17 | if t > array[x]: 18 | return binary_search_r(array, t, lastIndex, xmin, x) 19 | elif t < array[x]: 20 | return binary_search_r(array, t, lastIndex, x, xmax) 21 | else: 22 | return x 23 | 24 | def insertion_sort(array): 25 | sort = [array[0]] 26 | shifted = [] 27 | for i in range(1, len(array)): 28 | t = array[i] 29 | #check to see if t is higher than the last element in the sorted array 30 | if t >= sort[-1]: 31 | sort.append(t) 32 | continue 33 | # index = binary_search(sort, t) 34 | for j in range(0, len(sort)): 35 | if t < sort[j]: 36 | index = j 37 | break 38 | sort = sort[:index+1] + sort[index:] 39 | sort[index] = t 40 | return shifted 41 | # return sort 42 | 43 | n = int(input()) 44 | array = [int(x) for x in input().split()] 45 | sorted_array = insertion_sort(array) 46 | print(" ".join([str(x) for x in sorted_array])) -------------------------------------------------------------------------------- /BezierCurve.py: -------------------------------------------------------------------------------- 1 | ## input 2 | # 4 40 3 | # 501 458 4 | # 36 485 5 | # 492 746 6 | # 889 512 7 | ## answer 8 | # 501 458 467 461 437 464 410 468 386 473 366 478 348 484 334 491 323 498 314 505 308 513 305 520 305 528 307 536 311 544 317 552 326 559 337 566 349 573 364 580 380 586 398 591 417 596 438 600 460 604 484 606 509 608 534 609 561 608 588 607 617 604 646 600 675 594 705 588 735 579 766 569 797 558 828 544 858 529 889 512 9 | 10 | def calc_segments(points): 11 | segments = [] 12 | for i in range(len(points) - 1): 13 | x = points[i+1][0] - points[i][0] 14 | y = points[i+1][1] - points[i][1] 15 | segments.append((x,y)) 16 | return segments 17 | 18 | def calc_curve(points, n): 19 | delta_alpha = 1 / (n - 1) 20 | alpha = 0 21 | points_on_curve = [points[0]] 22 | for i in range(1, n): 23 | alpha += delta_alpha 24 | segments = calc_segments(points) 25 | temp_points = points[:] 26 | while len(temp_points) > 1: 27 | intermediate_segments = [] 28 | for j in range(0, len(segments)): 29 | x = temp_points[j][0] + segments[j][0] * alpha 30 | y = temp_points[j][1] + segments[j][1] * alpha 31 | intermediate_segments.append((x,y)) 32 | segments = calc_segments(intermediate_segments) 33 | temp_points = intermediate_segments[:] 34 | points_on_curve.append((round(temp_points[0][0]), round(temp_points[0][1]))) 35 | return points_on_curve 36 | 37 | def main(): 38 | (m,n) = (int(x) for x in input().split()) 39 | initial = [] 40 | for i in range(m): 41 | (x,y) = (int(z) for z in input().split()) 42 | initial.append((x,y)) 43 | points_on_curve = calc_curve(initial, n) 44 | 45 | for point in points_on_curve: 46 | print(point[0],point[1], end=' ') 47 | 48 | if __name__ == '__main__': 49 | main() -------------------------------------------------------------------------------- /MatchingBrackets.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 17 3 | # <<+>([[*]^{f}] [-])({%} )<+>> 4 | # <<[ ]a{[{b}(+)+[t( )]]<[%]a>c}>(f)>>{/}{^[f(b)]} 5 | # {e(w(}(-(-))(d{-}< {+}>)[{z}[(+)b]]) ) 6 | # ({ }<[%]/>{>v}{v}{h}) 7 | # ([ (+)(b[v]<%>)]){ }[<[z]< >z>[h{z}] [x][*]]({b}v)> 8 | # <{/(^{w )(d(t))}>(a))[u]>[e[*]{d}(v)] 9 | # [{z{c <[ ]x>c[d](w)(u){ }(-<*>)>}{*{d}{x}}}{-}] 10 | # <[a(y)]+{/} {e}[{[y{w}](*)e<< >y>}[/]][t{+}] 11 | # <>{x}{e ([f]{a}{%}b(t){+}) 12 | # [+[x]][[x]y{/}]({t(f){ }[w]}y{*{a}} h>)[] 13 | # <({*}z{{h}x})(+)[t]{*(d< >)[{ }(e)e]{d}}> 14 | # {b[a]}<{h}d>[v][[g( )]u[e]({)}uw]((b)) 15 | # [ ]{}( ({{z}f{x}}{g}c)(t{%(z)}(/){e}( ))) 16 | # <[%]{((-){^<(w)h>}g){h} (<*>{f} )}>[ ][v]{e[{/}v]}[t] 17 | # [v]([[/](+(%)(+))(w)(x{t})( [a])({{%]y}d[v]){a}e])[c(-)} 18 | # ([[^]{e}+]([g]e<->[y])[d(<{<*>x}<[*]*> >a)]) 19 | # <-(h)[v]>{t{a}}{d}[{v{-}} ][(+ {b}<*>t][([ <->]%)] 20 | 21 | def is_left_bracket(c): 22 | if c == '(' or c == '[' or c == '{' or c == '<': 23 | return True 24 | return False 25 | 26 | def is_right_bracket(c): 27 | if c == ')' or c == ']' or c == '}' or c == '>': 28 | return True 29 | return False 30 | 31 | dict = {')':'(', ']':'[', '}':'{', '>':'<'} 32 | 33 | n = int(input()) 34 | 35 | for i in range(0, n): 36 | line = input() 37 | stack = [] 38 | is_ok = True 39 | 40 | for c in line: 41 | if is_left_bracket(c): 42 | stack.append(c) 43 | elif is_right_bracket(c): 44 | if len(stack) > 0 and stack[-1] == dict[c]: 45 | stack.pop() 46 | else: 47 | is_ok = False 48 | break 49 | if is_ok and len(stack) == 0: 50 | print("1 ", end="") 51 | else: 52 | print("0 ", end="") -------------------------------------------------------------------------------- /FibonacciDivisibilityAdvanced.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 23 3 | # 9529 2701 7878 7278 3604 5778 2929 6225 9376 5551 6525 4728 5104 9452 6975 3226 3302 4875 7502 4429 4126 4053 2327 4 | import math 5 | 6 | def find_fib_until(fib_numbers, num): 7 | while True: 8 | a, b = fib_numbers[-2:] 9 | c = a + b 10 | fib_numbers.append(c) 11 | del fib_numbers[0] 12 | if c > num: 13 | break 14 | 15 | def find_next_fib(fib_numbers): 16 | for i in range(0, 100): 17 | a, b = fib_numbers[-2:] 18 | c = a + b 19 | fib_numbers.append(c) 20 | 21 | def is_fib(x): 22 | test1 = 5 * (x**2) + 4 23 | if (test1**.5).is_integer(): 24 | return True 25 | test2 = 5 * (x**2) - 4 26 | if (test2**.5).is_integer(): 27 | return True 28 | return False 29 | 30 | def find_position(Fn): 31 | n = math.log((Fn * math.sqrt(5) + math.sqrt(5 * (Fn*Fn) + 4))/2, (1 + math.sqrt(5))/2) 32 | print(n) 33 | 34 | def fib_index(Fn): 35 | Fn = float(Fn) 36 | phi = (1 + math.sqrt(5)) / 2 37 | x = Fn.__mul__(math.sqrt(5)).__add__((1/2)) 38 | 39 | return round(math.log(x) / math.log(phi)) 40 | 41 | # n = int(input()) 42 | # numbers = [int(x) for x in input().split()] 43 | 44 | n = 2 45 | numbers = [233328, 433156] 46 | 47 | fib_numbers = [0, 1] 48 | 49 | for i in range(0, n): 50 | num = numbers[i] 51 | 52 | j = 1 53 | while True: 54 | k = num * j 55 | if is_fib(k): 56 | print(k, fib_index(k)) 57 | break 58 | j += 1 59 | 60 | # found = False 61 | # fib_numbers = [0, 1] 62 | # find_fib_until(fib_numbers, num*num) 63 | # last_index = 2 64 | # while not found: 65 | # find_next_fib(fib_numbers) 66 | # for j in range(last_index, len(fib_numbers)): 67 | # if fib_numbers[j] % num == 0: 68 | # print(j, "", end="") 69 | # found = True 70 | # break 71 | # last_index = j -------------------------------------------------------------------------------- /TupperSelfReferentialFormula.py: -------------------------------------------------------------------------------- 1 | N = 4858450636189713423582095962494202044581400587983244549483093085061934704708809928450644769865524364849997247024915119110411605739177407856919754326571855442057210445735883681829823754139634338225199452191651284348332905131193199953502413758765239264874613394906870130562295813219481113685339535565290850023875092856892694555974281546386510730049106723058933586052544096664351265349363643957125565695936815184334857605266940161251266951421550539554519153785457525756590740540157929001765967965480064427829131488548259914721248506352686630476300 2 | H = 17 3 | W = 106 4 | 5 | import sys 6 | if __name__ == '__main__': 7 | if len(sys.argv)>1: H = int(sys.argv[1]) 8 | def tupper(x,y): 9 | return 0.5 < ((y//H) // (2**(H*x + y%H))) % 2 10 | 11 | print ("x range: 0 < x <",) 12 | W = int(input()) 13 | print('Got width: %d' % W) 14 | print("y range: N < y < N+%d, where N = (type 0 for default)" % H,) 15 | t = int(input()) 16 | if t: N=t 17 | print 18 | 19 | import matplotlib.pyplot as plot 20 | plot.rc('patch', antialiased=False) 21 | print('Plotting...') 22 | for x in range(W): 23 | print('Column %d...' % x) 24 | for yy in range(H): 25 | y = N + yy 26 | if tupper(x,y): 27 | plot.bar(left=x, bottom=yy, height=1, width=1, linewidth=0, color='black') 28 | print('Done plotting, please wait...') 29 | 30 | plot.axis('scaled') 31 | #For large graphs, must change these values (smaller font size, wider-apart ticks) 32 | buf = 2 33 | plot.xlim((-buf,W+buf)) 34 | plot.ylim((-buf,H+buf)) 35 | plot.rc('font', size=20) 36 | plot.xticks(range(0, W, 10)) 37 | yticks = range(0, H+1, 4) 38 | plot.yticks(yticks, ['N']+['N + %d'%i for i in yticks][1:]) 39 | plot.savefig('out.png') 40 | plot.savefig('out.svg') -------------------------------------------------------------------------------- /MaximumOfArray.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 9563 59114 -54932 48501 -65301 -75301 65794 518 72323 -52815 -39714 65537 -55115 -22581 -32780 30204 -57857 28574 35328 -19926 -52635 -43638 149 73410 903 -58837 27817 -24007 -76410 -26990 43898 13152 -47875 68965 -18347 -33176 73664 -32552 47341 65987 -5368 -72373 51525 19516 -14954 -61254 -30279 7189 47319 -74951 67262 74683 -38589 -12588 68094 42313 8575 15911 -61693 12164 68921 62204 -54683 -58954 51170 6970 -12131 44835 54417 -44790 30822 -30950 -37163 2348 68566 27882 21093 -41712 -44929 -11587 -36663 -57666 -16904 4747 9746 -28809 -32940 -61679 67101 -14633 30485 56022 -32429 55801 77068 -61258 -17229 -15063 63576 -42811 20147 14399 6239 62983 -63253 -5195 10866 37839 33093 45937 -53747 76429 68271 9349 1176 -1982 60539 48236 16339 47641 -46397 -33176 23664 1174 -57375 20732 19915 5396 -74331 3491 42585 25816 -62110 -31176 8800 -45363 43629 -60334 72475 -3278 65603 -61271 -6848 53875 28077 74327 -28107 8616 42564 68231 -23743 76166 -44944 79920 -2660 -22320 20652 -62744 63076 26321 20747 25662 -27863 38636 74485 60936 73273 38115 -79397 65748 -45162 66206 -75522 27989 40081 32554 22317 -68025 -38830 -15119 -79794 17426 -18953 -44739 17346 58387 12941 -42001 75643 -3982 64319 16390 -58320 -43543 -24973 -63834 -62606 -31700 54281 -62004 -45951 -70881 -75798 -41474 37108 44283 71079 -20575 56257 -47751 44305 56463 49675 -54647 -68275 -12978 -76259 24666 25020 79383 -59316 9340 15774 -37636 45797 70800 -21470 63190 -40900 -47189 -78814 -6851 -38071 -74612 31674 79036 49671 22753 -21538 25928 55002 -57232 2392 24678 -31879 14116 -68300 -28139 -41218 36720 -28755 -20534 -33939 67019 21829 -68142 57819 -79641 75047 -63080 -46831 76233 10068 -4902 -78378 -38257 -5865 51292 64496 52596 -2779 39498 75364 79612 -15824 -36515 13729 -4124 15346 52510 -47404 66591 -48024 -1343 53610 53805 10514 31430 54163 5562 48350 -72667 1796 -21582 2430 3417 20161 76565 -25290 4657 3 | 4 | array = input().split() 5 | print(max(int(i) for i in array), " ", end="") 6 | print(min(int(i) for i in array), " ", end="") -------------------------------------------------------------------------------- /TicTacToe.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 15 3 | # 2 4 3 6 7 9 1 5 8 4 | # 9 3 7 8 6 1 5 2 4 5 | # 1 4 9 5 6 3 2 8 7 6 | # 1 6 8 3 4 2 9 7 5 7 | # 7 4 6 5 1 9 3 8 2 8 | # 8 1 7 5 6 3 9 2 4 9 | # 2 4 3 9 7 8 5 1 6 10 | # 6 3 1 9 2 7 4 5 8 11 | # 1 4 7 6 8 9 5 3 2 12 | # 7 9 1 8 5 6 3 2 4 13 | # 1 3 9 6 8 2 5 7 4 14 | # 8 5 4 6 3 7 2 1 9 15 | # 7 2 4 5 8 1 9 3 6 16 | # 5 2 6 1 8 4 9 3 7 17 | # 4 8 5 3 2 6 1 7 9 18 | 19 | class Game: 20 | board = [] 21 | 22 | def initialize(self): 23 | self.board = [] 24 | for i in range(0, 3): 25 | self.board.append([None, None, None]) 26 | 27 | def mark(self, pos, p): 28 | self.board[pos // 3][pos % 3] = p 29 | 30 | def is_game_over(self): 31 | ##check rows 32 | for i in range(0, 3): 33 | if (self.board[i][0] == None): 34 | continue 35 | if (self.board[i][0] == self.board[i][1] 36 | and self.board[i][1] == self.board[i][2]): 37 | return True 38 | ##check columns 39 | for i in range(0, 3): 40 | if (self.board[0][i] == None): 41 | continue 42 | if (self.board[0][i] == self.board[1][i] 43 | and self.board[1][i] == self.board[2][i]): 44 | return True 45 | ##check diagonals 46 | if self.board[1][1] == None: 47 | return False 48 | if self.board[0][0] == self.board[1][1] and self.board[1][1] == self.board[2][2]: 49 | return True 50 | if self.board[2][0] == self.board[1][1] and self.board[1][1] == self.board[0][2]: 51 | return True 52 | return False 53 | 54 | game = Game() 55 | 56 | n = int(input()) 57 | 58 | for i in range(0, n): 59 | game.initialize() 60 | moves = [int(x) for x in input().split()] 61 | 62 | for j in range(0, len(moves)): 63 | player = None 64 | if j % 2 == 0: 65 | player = 'x' 66 | else: 67 | player = 'o' 68 | 69 | game.mark(moves[j] - 1, player) 70 | if game.is_game_over(): 71 | print(str(j+1), "", end="") 72 | break 73 | if j == len(moves) - 1: 74 | print(0, "", end="") -------------------------------------------------------------------------------- /FlyingTextScreensaver.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 40 18 7 3 | 4 | class ScreenSaver(): 5 | def __init__(self, width, height): 6 | self.width = width 7 | self.height = height 8 | self.x = 0 9 | self.y = 0 10 | self.left = False 11 | self.right = True 12 | self.up = False 13 | self.down = True 14 | 15 | def setText(self,text): 16 | self.text = text 17 | self.length = len(text) 18 | 19 | def update(self): 20 | if self.left: 21 | self.go_left() 22 | elif self.right: 23 | self.go_right() 24 | if self.up: 25 | self.go_up() 26 | elif self.down: 27 | self.go_down() 28 | 29 | def go_left(self): 30 | if self.x - 1 >= 0: 31 | self.x -= 1 32 | else: 33 | self.left = False 34 | self.right = True 35 | self.go_right() 36 | 37 | def go_right(self): 38 | if self.x + self.length < self.width: 39 | self.x += 1 40 | else: 41 | self.left = True 42 | self.right = False 43 | self.go_left() 44 | 45 | def go_up(self): 46 | if self.y - 1 >= 0: 47 | self.y -= 1 48 | else: 49 | self.up = False 50 | self.down = True 51 | self.go_down() 52 | 53 | def go_down(self): 54 | if self.y + 1 < self.height: 55 | self.y += 1 56 | else: 57 | self.up = True 58 | self.down = False 59 | self.go_up() 60 | 61 | def print(self): 62 | i = 0 63 | print('{}'.format('#' * (self.width + 2))) 64 | while i < self.height: 65 | if i == self.y: 66 | print('#{}{}{}#'.format(' ' * self.x, self.text, ' ' * (self.width - self.length - self.x))) 67 | else: 68 | print('#{}#'.format(' ' * self.width)) 69 | i += 1 70 | print('{}'.format('#' * (self.width + 2))) 71 | 72 | def main(): 73 | (width, height, length) = (int(x) for x in input().split()) 74 | 75 | ss = ScreenSaver(width, height) 76 | ss.setText('@' * length) 77 | 78 | for i in range(101): 79 | print(ss.x, ss.y, end=' ') 80 | ss.update() 81 | 82 | if __name__ == '__main__': 83 | main() -------------------------------------------------------------------------------- /SweetHarvest.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 17 3 | # 15 4 3 3 16 4 14 2 2 10 11 2 6 18 17 10 4 8 11 3 19 9 4 13 6 6 14 3 3 14 19 16 17 3 4 | # 13 5 13 14 6 4 5 6 9 3 4 17 6 10 8 8 9 16 10 3 2 15 15 4 16 10 4 13 7 5 11 19 8 5 5 | # 13 7 17 17 14 19 19 12 6 10 19 12 17 15 2 19 15 15 14 17 11 5 19 5 11 5 15 10 11 18 6 | # 4 6 2 2 19 2 2 12 6 10 11 17 8 6 17 7 2 13 2 18 5 5 7 | # 8 14 3 4 5 13 2 8 15 7 9 16 7 9 16 17 14 7 9 11 13 13 9 19 14 3 19 12 7 5 11 13 18 12 8 | # 4 5 16 10 19 4 17 15 9 7 12 7 2 17 14 11 11 8 19 10 2 2 10 13 7 13 4 19 12 15 15 14 9 | # 12 4 17 14 2 13 4 8 5 10 8 2 4 18 12 11 17 2 12 18 10 6 5 4 8 5 14 4 18 9 2 10 12 18 10 | # 12 12 8 19 15 16 7 16 19 6 8 11 3 9 3 19 17 7 5 2 14 8 15 11 | # 6 4 17 15 14 16 19 7 8 7 6 4 4 12 19 3 16 7 13 17 15 14 17 12 2 2 13 15 8 8 12 13 11 12 | # 8 5 7 7 10 14 13 15 17 15 7 16 17 3 4 10 18 17 5 16 10 6 16 3 19 4 10 13 | # 15 19 3 4 5 9 9 13 3 2 8 19 15 14 15 13 15 17 3 14 14 6 10 5 10 6 6 10 9 14 | # 3 4 14 5 6 18 13 14 11 14 15 18 13 11 13 9 4 8 6 5 2 19 10 11 4 19 15 9 10 4 4 15 | # 7 17 15 12 15 8 6 7 3 19 6 15 10 18 4 13 6 9 16 7 9 6 16 12 6 12 19 14 16 | # 4 6 2 2 2 13 16 8 17 4 10 17 9 5 8 7 8 19 12 16 16 17 5 3 14 15 7 6 15 19 19 17 | # 6 2 18 6 14 14 13 11 17 4 9 6 8 16 11 14 15 3 11 12 19 14 13 13 10 18 18 5 18 18 2 5 19 19 18 | # 13 13 3 4 10 6 12 15 12 8 7 7 4 9 16 14 8 10 8 2 19 7 18 4 5 16 5 19 | # 15 4 17 8 16 19 11 7 5 4 2 15 11 7 3 14 14 17 9 3 8 15 3 7 2 19 20 | 21 | def choose_best_path(isle, max_candies): 22 | rec(isle[1:], isle[0], max_candies) 23 | 24 | def rec(isle, score, max_candies): 25 | if len(isle) <= 1: 26 | max_candies.append(score) 27 | return 28 | if len(isle) >= 2: 29 | score1 = score + isle[1] 30 | rec(isle[2:], score1, max_candies) 31 | if len(isle) >= 3: 32 | score2 = score + isle[2] 33 | rec(isle[3:], score2, max_candies) 34 | 35 | n = int(input()) 36 | isles = [] 37 | 38 | for i in range(0, n): 39 | isle = [int(x) for x in input().split()] 40 | max_candies = [] 41 | choose_best_path(isle, max_candies) 42 | 43 | print(max(max_candies), "", end="") -------------------------------------------------------------------------------- /RockPaperScissors.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 28 3 | # RP PP RR PS RP 4 | # PR PP SR PS SS RR RS RP 5 | # PR SP PR RS RP PP RS 6 | # RP PS PS PR SS RR PP PR SP SP RR PR PS PS SP 7 | # RS PS RR PR RP PR RS SR SR RR PP PP SR RS RP 8 | # RR RR RR SP PR RP RR PP SP PS RR RS SP 9 | # SS RP RP PP SS RR RP PS SP RR RS SR RS RS SS PS 10 | # SR SP SR RP RP SS SP PP SS SS SS SS RP 11 | # SR PP RP PP RP SS SR PP RR RP 12 | # RR SS RP SR RP RP SS PP RR SS SP PR SS PR RS SR RP 13 | # PR SS PR SS SS PP RP RS 14 | # PR PP RS PR SP 15 | # RR PP RR RP RR RR RS PR RS RP RP RS 16 | # RR SR SR PS PR RP PP SS SS SP PR SR 17 | # PP SS PR RP SR RS SR PS 18 | # SR SS SP SS RR SR PR PR 19 | # RP RR RS PP SR PP SS PR PR SR SP PR PP SP 20 | # SP RP PP PP RS PR PS RR PR SP RS 21 | # SR PR PP SP RR SP SR RP PR 22 | # SS SS RR RS SP PR RP SP RR RS RP PS RS 23 | # PR PP SR PS RP 24 | # SR SR PR SR PR PP SS RP 25 | # RP SS PR PP RS PP RS RP PP RP PP SS SP PP PR 26 | # SP SS RR RP PS RS RS 27 | # PP SS RR RR SS SP RR SR RR RS RR SS PP RP PR 28 | # RP RS SP SS PS PS SS SR SS RS RR PS 29 | # RS RS RS SP RS PP SR RP SS PP SP 30 | # PP RP SS PS RP RS PR PR RR PS SP SP 31 | 32 | def convert_sign(s): 33 | if (s == 'R'): 34 | return 0 35 | elif (s == 'P'): 36 | return 1 37 | else: 38 | return 2 39 | 40 | def winner(seq): 41 | a = convert_sign(seq[0]) 42 | b = convert_sign(seq[1]) 43 | 44 | res = (a - b + 3) % 3 45 | 46 | if res == 0: 47 | return -1 48 | elif res == 1: 49 | return 0 50 | else: 51 | return 1 52 | 53 | def check_winner(s): 54 | player = [0,0] 55 | 56 | for i in range(0, len(s)): 57 | win = winner(s[i]) 58 | if win != -1: 59 | player[win] += 1 60 | 61 | if player[0] > player[1]: 62 | return 0 63 | elif player[0] < player[1]: 64 | return 1 65 | else: 66 | return -1 67 | 68 | def main(): 69 | n = int(input()) 70 | for i in range(0, n): 71 | seq = input().split() 72 | 73 | winner = check_winner(seq) 74 | 75 | if winner == -1: 76 | return 77 | 78 | print(winner + 1, "", end="") 79 | 80 | if __name__ == "__main__": 81 | main() -------------------------------------------------------------------------------- /BrainfuckInterpreter.py: -------------------------------------------------------------------------------- 1 | #input 2 | # ;>;<>;<>-<>;<[->++<][->+<]>:<:>;<;[->+>+<<]>>[-<<+>>]<<[->+<];>++++<>;<>;<>;<>;<+:>-:<;[->+>+<<]>>[-<<+>>]<<>-<>;<>:<>;<>:<[->+>++<<];+:>-:<:[>+<-]>++++<>:<>:<[->+>++<<]>-<[->++<][->+<]>-<:[->+<]+:>-:<:>: 3 | # 7 14 11 4 2 7 7 10 12 17 5 2 11 14 2 4 | 5 | class Brainfuck: 6 | def __init__(self): 7 | self.stack = [0] * 100 8 | self.pointer = 0 9 | 10 | def set_script(self, script): 11 | self.script = script 12 | 13 | def resize_stack(self): 14 | for i in range(0, len(self.stack)): 15 | self.stack.append(0) 16 | 17 | def set_input(self,inp): 18 | self.inp = inp.split() 19 | 20 | def interpret_token(self,t): 21 | if t == '+': 22 | self.stack[self.pointer] += 1 23 | elif t == '-': 24 | self.stack[self.pointer] -= 1 25 | elif t == ':': 26 | print(self.stack[self.pointer], end=' ') 27 | elif t == ';': 28 | self.stack[self.pointer] = int(self.inp.pop(0)) 29 | elif t == '>': 30 | self.pointer += 1 31 | if self.pointer >= len(self.stack) - 1: 32 | self.resize_stack() 33 | elif t == '<': 34 | self.pointer -= 1 35 | if self.pointer < 0: 36 | raise IndexError('Error: Data pointer is decremented below zero with "<" operation') 37 | 38 | def interpret(self,script): 39 | i = 0 40 | while i < len(script): 41 | t = script[i] 42 | if t == '[': 43 | while True: 44 | if self.stack[self.pointer] != 0: 45 | result = self.interpret(script[i+1:]) 46 | if result != None: 47 | end_index = script.index(result) 48 | else: 49 | break 50 | else: 51 | if end_index != None: 52 | i = end_index 53 | end_index = None 54 | else: 55 | j = i 56 | while script[j] != ']': 57 | j += 1 58 | i = j 59 | break 60 | elif t == ']': 61 | return script[i:] 62 | else: 63 | self.interpret_token(t) 64 | i += 1 65 | 66 | def main(): 67 | bf = Brainfuck() 68 | script = input() 69 | bf.set_input(input()) 70 | bf.interpret(script) 71 | 72 | if __name__ == '__main__': 73 | main() -------------------------------------------------------------------------------- /LuhnAlgorithm.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 45 3 | # 2457936494652626 4 | # 97?4007043638635 5 | # 1383223592?19814 6 | # 9395692538403002 7 | # 405462418?345881 8 | # 9472573944710938 9 | # 43?3987686879416 10 | # 2168466840113397 11 | # ?599469437091510 12 | # 5958352483344315 13 | # 2642476956082118 14 | # 6473415214792624 15 | # 28127311?5484100 16 | # 9227858484323065 17 | # 71738796155953?1 18 | # ?339050987742616 19 | # 8490248467735171 20 | # 8213172347506405 21 | # 6098933888371907 22 | # 4241754159?11181 23 | # 5534314125571464 24 | # 2?25405427772498 25 | # 7388680925755520 26 | # 20035408?2444772 27 | # 1618393556438909 28 | # 1?41890293636382 29 | # 54926582329?0325 30 | # 6635430790034167 31 | # 2105674206038953 32 | # 9002545672047379 33 | # 1975?60161120392 34 | # 44?7150269128544 35 | # 6873597103826488 36 | # 31?5462150267707 37 | # 99815398?5769563 38 | # 8852439461195853 39 | # 50?4168617952405 40 | # 6175588802114733 41 | # 7809398653901945 42 | # 7266231702578582 43 | # 5917970014697137 44 | # 4143254325523211 45 | # 6055195471726484 46 | # 9770745295817222 47 | # 5373564995382308 48 | 49 | def checksum(num): 50 | i = len(num) - 1 51 | second = False 52 | csum = 0 53 | while i >= 0: 54 | if second: 55 | temp = int(num[i]) * 2 56 | if temp > 9: 57 | temp -= 9 58 | csum += temp 59 | else: 60 | csum += int(num[i]) 61 | second = not second 62 | i -= 1 63 | return csum 64 | 65 | def is_valid(csum): 66 | if csum % 10 == 0: 67 | return True 68 | return False 69 | 70 | def find_valid_card_number(num): 71 | if num.count('?') > 0: 72 | i = 0 73 | while i < 10: 74 | temp_num = num[:] 75 | temp_num = temp_num.replace('?', str(i)) 76 | csum = checksum(temp_num) 77 | if is_valid(csum): 78 | return temp_num 79 | else: 80 | i += 1 81 | else: 82 | i = 0 83 | while i < len(num) - 1: 84 | temp_num = list(num[:]) 85 | t = temp_num[i] 86 | temp_num[i] = temp_num[i+1] 87 | temp_num[i+1] = t 88 | csum = checksum(temp_num) 89 | if is_valid(csum): 90 | return ''.join(temp_num) 91 | else: 92 | i += 1 93 | 94 | def main(): 95 | n = int(input()) 96 | for i in range(n): 97 | card = input() 98 | fixed_num = find_valid_card_number(card) 99 | print(fixed_num, end=' ') 100 | 101 | if __name__ == '__main__': 102 | main() 103 | -------------------------------------------------------------------------------- /RotationIn2DSpace.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 32 6 3 | # Castor 551 641 4 | # Procyon -922 -520 5 | # Deneb -459 -741 6 | # Mizar -221 -209 7 | # Mira -34 -614 8 | # Sirius -960 -76 9 | # Alcor -76 339 10 | # Heka 733 631 11 | # Betelgeuse -814 -641 12 | # Nembus -433 -71 13 | # Thabit 53 -6 14 | # Media -493 -567 15 | # Gemma 425 -418 16 | # Yildun -879 463 17 | # Jabbah -135 -903 18 | # Kochab 495 -585 19 | # Bellatrix 738 574 20 | # Diadem -104 -721 21 | # Altair 834 675 22 | # Unuk 70 -200 23 | # Rigel -940 111 24 | # Kastra 724 -15 25 | # Capella -551 456 26 | # Polaris -384 -364 27 | # Zosma 816 183 28 | # Alcyone 565 -131 29 | # Electra -824 -928 30 | # Fomalhaut 302 602 31 | # Albireo -346 423 32 | # Pherkad 64 519 33 | # Lesath 521 -441 34 | # Vega 934 258 35 | 36 | import math 37 | 38 | def normalize(v): 39 | length = (v[0]**2 + v[1]**2)**.5 40 | normalized_v = (v[0]/length, v[1]/length) 41 | return normalized_v 42 | 43 | def cross_product(a, b): 44 | cp = a[0] * b[1] - b[0] * a[1] 45 | return abs(cp) 46 | 47 | def dot_product(a, b): 48 | dp = a[0] * b[0] + a[1] * b[1] 49 | return dp 50 | 51 | def rotation_matrix_degree(degree): 52 | rad = degree * math.pi/180 53 | R = (normalize((math.cos(rad), -math.sin(rad))), normalize((math.sin(rad), math.cos(rad)))) 54 | return R 55 | 56 | def multiply_mv(m, v): 57 | result = [0,0] 58 | for i in range(0, 2): 59 | for j in range(0, 2): 60 | result[i] += m[i][j] * v[j] 61 | return tuple(result) 62 | 63 | def transform_vector_degree(v, degree): 64 | R = rotation_matrix_degree(degree) 65 | mv = multiply_mv(R, v) 66 | return mv 67 | 68 | def sort_by_y_asc(v): 69 | e = 1 70 | while e: 71 | e = 0 72 | for i in range(0, len(v) - 1): 73 | if v[i][2] > v[i+1][2]: 74 | t = v[i] 75 | v[i] = v[i+1] 76 | v[i+1] = t 77 | e = 1 78 | elif v[i][2] == v[i+1][2]: 79 | if v[i][1] > v[i+1][1]: 80 | t = v[i] 81 | v[i] = v[i+1] 82 | v[i+1] = t 83 | e = 1 84 | return v 85 | 86 | (n, angle) = (int(x) for x in input().split()) 87 | points = [] 88 | 89 | for i in range(0, n): 90 | (name, x, y) = (x for x in input().split()) 91 | x = int(x) 92 | y = int(y) 93 | v = transform_vector_degree((x,y), angle) 94 | v = (round(v[0]), round(v[1])) 95 | points.append([name, v[0], v[1]]) 96 | 97 | sorted_points = sort_by_y_asc(points) 98 | 99 | for p in sorted_points: 100 | print(p[0], "", end="") -------------------------------------------------------------------------------- /FibonacciSequence.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 20 3 | # 1777930954809416587147660791784794314784432111526800706093789579403138960940165075820050317562202766948028237512 4 | # 5689768398165682472981133878451278523009637608647762675604795738876774718113916506327804992150205611581315356832469416472592015530113139666919895048261416544213718823613767148595520981851577168 5 | # 1593326225701717188334037111425359127138512324945743711294024460075377172985524819472680355872170395569016093752628516876254232560412670420129021724057156769422272151448461996634558399312613 6 | # 668996615388005031531000081241745415306766517246774551964595292186469 7 | # 2830653773025598082345063352442424920351144475210443140761432888315880232178889808908956305371077582723774166955957876499339886412043139544385164571854387045842521 8 | # 897889194859191704881857622613605161659692872156509128465291624947856903121114331700554055405737435019936805984303748490745 9 | # 5789092068864820527338372482892113982249794889765 10 | # 102334155 11 | # 132980473367242282497284673037549604307310746277363901731233717012104672704818538889393037469151708867132489255106919564710136837304160825061948265664510600390158933 12 | # 167889621328187018603839571160601156165718032465198173590271441192035550962902993642892477664171488276167058117358975773751425901845612997090658 13 | # 11111460156937785151929026842503960837766832936 14 | # 187341518601536966291015050946540312701895836604078191803255601777 15 | # 43566776258854844738105 16 | # 10108265416152526419683994794618270268165872518704428380856874159529924875319101159659894110659650591571332238987107046525767189192225493851401061174799065698263103347 17 | # 40232462861844090389128434238541564732364078131780448061576898306103009199405081373822175980623530127985951663375242165249073319332045062588388761317543568249014325104512245165644739010 18 | # 4953967011875066473162524925231604047727791871346061001150551747313593851366517214899257280600 19 | # 3516470258181436632779942061407967017889567600656021732351687343954813226146279635109807764516348660489138275647928031738863616520008909996193084872671097173635385035177795631888400653504011257 20 | # 941390895042587567453271223806288165311401367715034229502159202 21 | # 1645645409178311156114050175340179094658577397657624573049761120640548215334513341070281 22 | # 139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125 23 | 24 | def find_fib(fib_numbers, num): 25 | while True: 26 | a, b = fib_numbers[-2:] 27 | c = a + b 28 | fib_numbers.append(c) 29 | 30 | if num == c: 31 | break 32 | 33 | n = int(input()) 34 | 35 | fib_numbers = [0, 1] 36 | 37 | for i in range(0, n): 38 | num = int(input()) 39 | if num not in fib_numbers: 40 | find_fib(fib_numbers, num) 41 | print(fib_numbers.index(num), "", end="") -------------------------------------------------------------------------------- /CaesarCipherCracker.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 5 3 | # DANA ZEAO EJ IU XKOOKI PQNJO WHH WNKQJZ XQP ZKAO JKP IKRA CNAAJBEAHZO WNA CKJA JKS 4 | # NF RNFL NF YLVAT VG VF OYNPX NAQ JUVGR NAQ ERQ NYY BIRE VS OYBBQ OR GUR CEVPR BS GUR NQZVENYGL 5 | # OP TPPOFS TQPLFO UIBO CSPLFO GPVS TDPSF BOE TFWFO ZFBST BHP UP VT JO PMEFO TUPSJFT 6 | # YWHHAZ EP PDA NEOEJC OQJ EJ WJYEAJP LANOEW PDANA SWO W GEJC SDK SWJPO PK HERA BKNARAN 7 | # PX VTEE AXK MAX PHFTG PAH WBW GHM VTKX LMHGX PTEEL WH GHM T IKBLHG FTDX 8 | 9 | #generates a new dict ratated by k palces 10 | def generate_dict(alphabet, k): 11 | dict = {} 12 | for i in range(0, len(alphabet)): 13 | dict[alphabet[(i+k) % len(alphabet)]] = alphabet[i] 14 | return dict 15 | 16 | #calculates the letter frequency for given sentence 17 | def calculate_letter_freq(sentence, alphabet): 18 | freq = [0] * len(alphabet) 19 | for i in range(0, len(alphabet)): 20 | freq[i] = sentence.count(alphabet[i]) / len(sentence) * 100 21 | return freq 22 | 23 | #calculates the difference in distribution between found distribution 24 | #and ideal distribution 25 | def distribution_difference(freq, ideal_letter_distribution): 26 | diff = [] 27 | for i in range(0, len(freq)): 28 | diff.append(freq[i] - ideal_letter_distribution[i]) 29 | return diff 30 | 31 | #decipheres a given sentence, returns the rotation k 32 | def decipher(sentence, alphabet, ideal_letter_distribution): 33 | k = 0 34 | deciphered_sentences = [] 35 | differences = [] 36 | for i in range(0, 25): 37 | deciphered = "" 38 | dict = generate_dict(alphabet, k) 39 | k += 1 40 | 41 | for c in sentence: 42 | if c != ' ': 43 | deciphered += dict[c] 44 | else: 45 | deciphered += c 46 | deciphered_sentences.append(deciphered) 47 | 48 | freq = calculate_letter_freq(deciphered, alphabet) 49 | diff = distribution_difference(freq, ideal_letter_distribution) 50 | diff = [x**2 for x in diff] 51 | differences.append(diff) 52 | 53 | averages = [] 54 | for i in range(0, len(differences)): 55 | diff_sum = sum(differences[i]) 56 | average = diff_sum / len(differences[i]) 57 | averages.append(average) 58 | 59 | min_average_index = 0 60 | for i in range(0, len(averages)): 61 | if averages[i] < averages[min_average_index]: 62 | min_average_index = i 63 | 64 | return deciphered_sentences, min_average_index 65 | 66 | 67 | alphabet = [c for c in 'abcdefghijklmnopqrstuvwxyz'.upper()] 68 | ideal_letter_distribution = [8.1, 1.5, 2.8, 4.3, 13.0, 2.2, 2.0, 6.1, 7.0, 0.15, 0.77, 7.0, 2.4, 69 | 6.8, 7.5, 1.9, 0.095, 6.0, 6.3, 9.1, 2.8, 0.98, 2.4, 0.15, 2.0, 0.074] 70 | 71 | n = int(input()) 72 | 73 | for i in range(0, n): 74 | sentence = input() 75 | 76 | deciphered_sentences, k = decipher(sentence, alphabet, ideal_letter_distribution) 77 | 78 | print(' '.join((deciphered_sentences[k].split())[:3]), k, "", end="") -------------------------------------------------------------------------------- /GameOf2048.py: -------------------------------------------------------------------------------- 1 | #input 2 | # 2 4 4 2 3 | # 2 2 2 2 4 | # 2 2 2 2 5 | # 4 2 2 4 6 | # R R R D L R U D U 7 | 8 | class Tile: 9 | value = 0 10 | alreadyStacked = False 11 | def __init__(self, _value=0): 12 | self.value = _value 13 | 14 | class Game: 15 | board = [] 16 | 17 | def up(self): 18 | for i in range(0, 4): 19 | for j in range(0, 4): 20 | if self.board[i][j].value != 0: 21 | k = i 22 | while k > 0: 23 | tile = self.board[k][j] 24 | if self.board[k-1][j].value == 0: 25 | self.board[k-1][j] = tile 26 | self.board[k][j] = Tile() 27 | elif self.board[k-1][j].value == tile.value and not self.board[k-1][j].alreadyStacked and not self.board[k][j].alreadyStacked: 28 | self.board[k-1][j].value = tile.value * 2 29 | self.board[k-1][j].alreadyStacked = True 30 | self.board[k][j] = Tile() 31 | k -= 1 32 | self.resetStacks() 33 | 34 | def down(self): 35 | for i in range(3, -1, -1): 36 | for j in range(3, -1, -1): 37 | if self.board[i][j].value != 0: 38 | k = i 39 | while k < 3: 40 | tile = self.board[k][j] 41 | if self.board[k+1][j].value == 0: 42 | self.board[k+1][j] = tile 43 | self.board[k][j] = Tile() 44 | elif self.board[k+1][j].value == tile.value and not self.board[k+1][j].alreadyStacked and not self.board[k][j].alreadyStacked: 45 | self.board[k+1][j].value = tile.value * 2 46 | self.board[k+1][j].alreadyStacked = True 47 | self.board[k][j] = Tile() 48 | k += 1 49 | self.resetStacks() 50 | 51 | def right(self): 52 | for j in range(3, -1, -1): 53 | for i in range(3, -1, -1): 54 | if self.board[i][j].value != 0: 55 | k = j 56 | while k < 3: 57 | tile = self.board[i][k] 58 | if self.board[i][k+1].value == 0: 59 | self.board[i][k+1] = tile 60 | self.board[i][k] = Tile() 61 | elif self.board[i][k+1].value == tile.value and not self.board[i][k+1].alreadyStacked and not self.board[i][k].alreadyStacked: 62 | self.board[i][k+1].value = tile.value * 2 63 | self.board[i][k+1].alreadyStacked = True 64 | self.board[i][k] = Tile() 65 | k += 1 66 | self.resetStacks() 67 | 68 | def left(self): 69 | for j in range(0, 4): 70 | for i in range(0, 4): 71 | if self.board[i][j].value != 0: 72 | k = j 73 | while k > 0: 74 | tile = self.board[i][k] 75 | if self.board[i][k-1].value == 0: 76 | self.board[i][k-1] = tile 77 | self.board[i][k] = Tile() 78 | elif self.board[i][k-1].value == tile.value and not self.board[i][k-1].alreadyStacked and not self.board[i][k].alreadyStacked: 79 | self.board[i][k-1].value = tile.value * 2 80 | self.board[i][k-1].alreadyStacked = True 81 | self.board[i][k] = Tile() 82 | k -= 1 83 | self.resetStacks() 84 | 85 | def resetStacks(self): 86 | for i in self.board: 87 | for j in i: 88 | j.alreadyStacked = False 89 | 90 | def printBoard(self): 91 | for i in range(0, 4): 92 | for j in range(0, 4): 93 | print(self.board[i][j].value, "", end="") 94 | print() 95 | print() 96 | 97 | def countValues(self): 98 | values = {2:0, 4:0, 8:0, 16:0, 32:0, 64:0, 128:0, 256:0, 512:0, 1024:0, 2048:0} 99 | for i in self.board: 100 | for j in i: 101 | if j.value in values: 102 | values[j.value] += 1 103 | sortedKeys = sorted(values.keys()) 104 | 105 | for k in range(len(sortedKeys)-1, 0, -1): 106 | if values[sortedKeys[k]] == 0: 107 | del sortedKeys[k] 108 | else: 109 | break 110 | 111 | for k in range(0, len(sortedKeys)): 112 | print(values[sortedKeys[k]], "", end="") 113 | 114 | def solve(self): 115 | temp_board = [] 116 | for i in range(0, 4): 117 | row = [Tile(int(x)) for x in input().split()] 118 | temp_board.append(row) 119 | self.board = temp_board 120 | 121 | instructions = input().split() 122 | for i in instructions: 123 | if i == 'U': 124 | self.up() 125 | elif i == 'D': 126 | self.down() 127 | elif i == 'R': 128 | self.right() 129 | elif i == 'L': 130 | self.left() 131 | 132 | self.countValues() 133 | 134 | game = Game() 135 | game.solve() -------------------------------------------------------------------------------- /WanderingStarSigDiff.py: -------------------------------------------------------------------------------- 1 | import math 2 | import operator 3 | import random 4 | 5 | class Problem: 6 | __lengtha = 0 7 | __lengthb = 0 8 | __initial_vectora = [] 9 | __initial_vectorb = [] 10 | 11 | def getInitialVectorA(self): 12 | return self.__initial_vectora 13 | def setInitialVectorA(self,a): 14 | self.__initial_vectora = a 15 | self.__lengtha = len(self.__initial_vectora) 16 | def getInitialVectorB(self): 17 | return self.__initial_vectorb 18 | def setInitialVectorB(self,b): 19 | self.__initial_vectorb = b 20 | self.__lengthb = len(self.__initial_vectorb) 21 | def getLengthA(self): 22 | return self.__lengtha 23 | def getLengthB(self): 24 | return self.__lengthb 25 | 26 | def distance_between_points(a, b): 27 | dist = ((b[0] - a[0])**2 + (b[1] - a[1])**2)**.5 28 | return dist 29 | 30 | def calculate_signature(array): 31 | signature = [] 32 | for i in range(0, len(array)): 33 | current_vector = array[i] 34 | distances = [] 35 | for j in range(0, len(array)): 36 | dist = distance_between_points(current_vector, array[j]) 37 | if dist == 0: 38 | continue 39 | distances.append([dist, array[j]]) 40 | distances = sorted(distances, key=operator.itemgetter(0)) 41 | 42 | dists = [x[0] for x in distances[:10]] 43 | points = [x[1] for x in distances[:10]] 44 | 45 | signature.append([current_vector, dists, points])#, distances[:10][1]]) 46 | return signature 47 | 48 | def score(a, b): 49 | if a == b: 50 | return 0 51 | else: 52 | return (a-b)**2 53 | 54 | ##init problem 55 | problem = Problem() 56 | problem.setInitialVectorA([(-10.1, -20.8), (-16.8, -12.5), (1.0, 26.8), (11.5, 44.7), (-3.8, -47.5), (36.7, -23.1), (-9.5, 19.5), (13.0, -42.1), (-9.9, 42.2), (16.0, 35.5), (-2.2, 47.0), (16.6, -46.3), (1.2, 26.0), (-28.3, -35.3), (-13.7, 42.8), (-28.7, -3.1), (-11.3, 13.3), (33.2, 21.8), (-16.4, 0.4), (8.1, -11.5), (31.3, -34.6), (46.4, 3.5), (5.4, 38.8), (-33.2, -26.2), (-44.6, 15.0), (-6.8, 46.2), (-19.0, 43.7), (9.9, 9.9), (34.5, 8.1), (-14.2, 21.5), (8.3, 42.3), (16.4, 21.7), (9.7, -47.7), (38.7, -25.8), (43.6, 4.1), (22.5, -42.9), (20.6, -32.6), (29.6, -24.3), (12.1, 44.9), (4.3, 27.6), (41.8, 8.2), (-3.0, 42.0), (38.0, -28.2), (41.4, 19.9), (-3.9, 49.5), (-11.5, -22.2), (38.0, -27.8), (30.4, -21.5), (15.5, -5.6), (-42.5, -4.2), (26.7, -16.2), (28.9, -31.5), (27.5, 25.5), (35.6, 29.1), (-0.5, -46.7), (0.9, -39.2), (-9.5, -14.1), (31.5, -26.8), (28.5, 33.8), (-3.5, 45.3), (-28.5, 0.3), (24.5, 40.0), (26.0, 32.2), (27.5, 15.2), (-2.2, 30.8), (16.3, -6.8), (11.1, -39.5), (31.1, -15.0), (8.0, -17.9), (33.3, -31.9), (-9.9, 41.9), (18.0, -23.3), (37.1, 17.0), (6.2, -1.6), (9.6, -9.6), (36.0, 4.8), (-15.5, -23.4), (36.9, -18.2), (19.9, 13.4), (-11.1, 23.8), (35.1, 7.8), (27.9, -9.4), (12.8, 2.9), (9.4, 9.7), (-38.3, 25.8), (16.4, 1.1), (30.3, 17.1), (11.0, 5.7), (-17.9, 39.2), (-25.7, -24.1), (14.4, 17.9), (26.6, -12.7), (-0.8, 16.1), (31.4, -17.4), (-0.7, 29.3), (-44.1, 7.1), (31.4, 18.5), (10.3, -36.9), (-20.3, 23.4), (17.3, -34.9), (43.8, 17.6), (-39.6, -29.1)]) 57 | problem.setInitialVectorB([(9.0, -6.8), (37.1, 30.8), (-5.1, -45.3), (20.2, -36.0), (12.1, -15.9), (14.5, -47.8), (16.7, 39.3), (-23.2, 40.4), (44.8, -15.8), (21.2, -26.5), (-46.5, -9.0), (-14.2, -36.8), (-19.1, -42.6), (16.8, -2.8), (23.1, -32.9), (-9.7, 6.2), (1.2, 14.8), (45.9, 13.0), (18.3, 23.5), (24.9, -40.5), (-4.9, -39.5), (6.2, -40.8), (-37.6, -19.0), (29.4, 8.5), (40.8, -1.7), (-43.7, 20.0), (-16.6, -15.9), (8.5, -31.5), (-20.6, -15.0), (27.5, -36.4), (45.5, 20.4), (-5.5, -42.1), (-27.0, 32.8), (-45.5, -6.9), (25.7, 39.6), (36.5, 30.9), (42.9, 12.8), (17.7, 25.6), (8.2, 40.6), (-1.7, 28.1), (14.0, -44.0), (6.5, 23.9), (16.5, 39.0), (19.2, -9.4), (38.8, -12.9), (18.5, -41.2), (21.8, 19.6), (-22.1, 9.2), (39.2, -2.4), (17.0, -6.1), (28.3, -0.5), (38.4, -12.3), (11.7, 12.1), (38.1, -15.9), (-22.4, -41.7), (35.9, 20.7), (-16.1, 39.1), (-10.8, -9.9), (8.0, -15.4), (23.7, 38.8), (17.4, 1.5), (2.5, -21.8), (2.2, -41.2), (-34.6, 15.1), (-37.4, -8.7), (18.5, 20.5), (-10.7, -48.6), (18.1, -38.1), (32.5, 30.4), (22.6, 35.7), (44.3, -20.3), (2.7, 23.4), (28.3, 28.8), (-14.7, -15.4), (-5.5, 47.9), (-46.0, 0.0), (15.2, -16.3), (-22.0, 43.5), (24.0, -30.7), (-30.4, 25.7), (25.5, 42.7), (45.1, -5.9), (17.9, 1.4), (9.5, 45.1), (-18.5, 44.0), (21.3, 41.2), (5.8, -16.3), (-16.4, -4.9), (40.9, 6.3), (-29.9, -10.5), (17.6, -46.5), (44.0, 0.2), (35.8, -2.7), (-44.4, -8.0), (5.8, 19.3), (25.8, 6.1), (-34.9, -34.6), (-20.2, 12.0), (13.6, 41.7), (24.0, -24.3), (19.6, -29.6), (18.3, 19.7)]) 58 | 59 | ##calculate the signatures 60 | # [vector,[signature..], [to_vector..]] 61 | signaturea = calculate_signature(problem.getInitialVectorA()) 62 | signatureb = calculate_signature(problem.getInitialVectorB()) 63 | 64 | # print(signaturea) 65 | # print(signatureb) 66 | 67 | 68 | ## 69 | def compare_signatures(signaturea, signatureb): 70 | # sig_lista will be X 71 | sig_lista = signaturea[:] 72 | # sig_listb will be Y 73 | sig_listb = signatureb[:] 74 | 75 | whichList = None 76 | prevWhichList = None 77 | prev = None 78 | Bm, Bf = 0, 0 79 | p = 20 80 | firstIteration = True 81 | # while X and Y are not empty 82 | 83 | while len(sig_lista) + len(sig_listb) != 0: 84 | # Call the element at the front of X x, and the element at the front of Y y. 85 | # Let z be the smaller of x and y, and set whichList to the ID of the list (X or Y) that z came from. 86 | if len(sig_lista) > 0: 87 | x = sig_lista[0] 88 | else: 89 | x = float('inf') 90 | if len(sig_listb) > 0: 91 | y = sig_listb[0] 92 | else: 93 | y = float('inf') 94 | 95 | if x < y: 96 | z = x 97 | whichList = sig_lista 98 | # elif y < x: 99 | else: 100 | z = y 101 | whichList = sig_listb 102 | 103 | # Remove the first element (z) from the list named by whichList. 104 | del whichList[0] 105 | 106 | # If whichList == prevWhichList (i.e. if the previous smallest number, 107 | # prev, was also from the same list as z), or if this is the first iteration, 108 | # then set newBm = min(Bf + p, Bm) + p. 109 | if whichList == prevWhichList or firstIteration: 110 | if (firstIteration): 111 | newBm = min(Bf + p, Bm) 112 | else: 113 | newBm = min(Bf + p, Bm) + p 114 | # Otherwise, it's possible to match z with the previous number written out, 115 | # so set newBm = Bf + score(z, prev). 116 | else: 117 | newBm = Bf + score(z, prev) 118 | 119 | # Set Bf = min(Bf + p, Bm). 120 | Bf = min(Bf + p, Bm) 121 | 122 | #Set Bm = newBm. 123 | Bm = newBm 124 | 125 | # Set prev = z and prevWhichList = whichList. 126 | prev = z 127 | prevWhichList = whichList 128 | 129 | firstIteration = False 130 | 131 | # If X and Y are both empty, then halt. The final score is min(Bf + p, Bm) 132 | final_score = min(Bf + p, Bm) 133 | print(final_score) 134 | return final_score 135 | 136 | def do_compare_signatures(signaturea, signatureb): 137 | for i in range(0, len(signaturea)): 138 | for j in range(0, len(signatureb)): 139 | # [vector,[signature..], [to_vector..]] 140 | score = compare_signatures(signaturea[i][1][:], signatureb[j][1][:]) 141 | # break 142 | 143 | 144 | ## TEST 145 | a = [10, 20, 30, 40] 146 | b = [11, 18, 41, 50] 147 | c = [20, 30, 40, 10] 148 | compare_signatures(a, b) 149 | compare_signatures(a, a) 150 | compare_signatures(a, c) 151 | ## 152 | # do_compare_signatures(signaturea, signatureb) -------------------------------------------------------------------------------- /WanderingStar.py: -------------------------------------------------------------------------------- 1 | import math 2 | import copy 3 | 4 | def distance_between_points(a, b): 5 | dist = ((b[0] - a[0])**2 + (b[1] - a[1])**2)**.5 6 | return dist 7 | 8 | n0 = 94 9 | n1=92 10 | xy0 = [[-47.5, -10.4],[19.1, 25.9],[18.9, -10.4],[-2.1, -47.6],[41.8, -12.1],[-15.7, 12.1],[-11.0, -0.6],[-15.6, -7.6],[14.9, 43.5],[16.6, 0.1],[3.6, -33.5],[-14.2, 20.8],[17.8, -29.8],[-2.2, -12.8],[44.6, 19.7],[17.9, -41.3],[24.6, 37.0],[43.9, 14.5],[23.8, 19.6],[-4.2, -40.5],[32.0, 17.2],[22.6, -26.9],[9.9, -33.4],[-13.6, 6.6],[48.5, -3.5],[-9.9, -39.9],[-28.2, 20.7],[7.1, 15.5],[-36.2, -29.9],[-18.2, 11.1],[-1.2, -13.7],[9.3, 9.3],[39.2, 15.8],[-5.2, -16.2],[-34.9, 5.0],[-13.4, -31.8],[24.7, -29.1],[1.4, 24.0],[-24.4, 18.0],[11.9, -29.1],[36.3, 18.6],[30.3, 38.4],[4.8, -20.5],[-46.8, 12.1],[-44.2, -6.0],[-1.4, -39.7],[-1.0, -13.7],[13.3, 23.6],[37.4, -7.0],[-22.3, 37.8],[17.6, -3.3],[35.0, -9.1],[-44.5, 13.1],[-5.1, 19.7],[-12.1, 1.7],[-30.9, -1.9],[-19.4, -15.0],[10.8, 31.9],[19.7, 3.1],[29.9, -16.6],[31.7, -26.8],[38.1, 30.2],[3.5, 25.1],[-14.8, 19.6],[2.1, 29.0],[-9.6, -32.9],[24.8, 4.9],[-2.2, -24.7],[-4.3, -37.4],[-3.0, 37.4],[-34.0, -21.2],[-18.4, 34.6],[9.3, -45.2],[-21.1, -10.3],[-19.8, 29.1],[31.3, 37.7],[27.2, 19.3],[-1.6, -45.6],[35.3, -23.5],[-39.9, -19.8],[-3.8, 40.6],[-15.7, 12.5],[-0.8, -16.3],[-5.1, 13.1],[-13.8, -25.7],[43.8, 5.6],[9.2, 38.6],[42.2, 0.2],[-10.0, -48.6],[14.1, -6.5],[34.6, -26.8],[11.1, -6.7],[-6.1, 25.1],[-38.3, 8.1]] 11 | xy1 = [[-14.8, 10.9],[18.8, -0.1],[-11.3, 5.7],[-19.7, 6.9],[-11.5, -16.7],[-45.4, -15.3],[6.0, -46.9],[-24.1, -26.3],[30.2, 27.4],[21.4, -27.2],[12.1, -36.1],[23.8, -38.7],[41.5, 5.3],[-8.7, 25.5],[36.6, -5.9],[43.7, -14.6],[-9.7, -8.6],[34.7, -19.3],[-15.5, 19.3],[21.4, 3.9],[34.0, 29.8],[6.5, 19.5],[28.2, -21.7],[13.4, -41.8],[-25.9, -6.9],[37.5, 27.8],[18.1, 44.7],[-43.0, -19.9],[-15.7, 18.0],[2.4, -31.6],[9.6, -37.6],[15.4, -28.8],[43.6, -11.2],[4.6, -10.2],[-8.8, 38.2],[8.7, -34.6],[-4.7, 14.1],[-1.7, 31.3],[0.6, 27.9],[26.3, 13.7],[-1.2, 26.3],[32.1, -17.7],[15.5, 32.6],[-14.4, -12.6],[22.3, -22.5],[7.0, 48.5],[-6.4, 20.5],[-42.9, 4.2],[-23.0, 31.6],[-24.6, 14.0],[-30.2, -26.5],[-29.0, 15.7],[6.0, 36.3],[44.3, 13.5],[-27.6, 33.7],[13.4, -43.9],[10.5, 28.9],[47.0, 1.4],[10.2, 14.0],[13.3, -15.9],[-3.4, -25.6],[-14.7, 10.5],[21.6, 27.6],[21.8, 10.6],[-37.8, -14.2],[7.6, -21.8],[-8.6, 1.3],[6.8, -13.3],[40.9, -15.3],[-10.3, 41.1],[6.0, -10.8],[-1.5, -31.4],[-35.6, 1.0],[2.5, -14.3],[24.4, -2.6],[-24.1, -35.3],[-29.9, -34.7],[15.9, -1.0],[19.5, 7.0],[44.5, 19.1],[39.7, 2.7],[2.7, 42.4],[-23.0, 25.9],[25.0, 28.2],[31.2, -32.8],[3.9, -38.4],[-44.8, 2.7],[-39.9, -19.3],[-7.0, -0.6],[5.8, -10.9],[-44.5, 19.9],[-31.5, -1.2]] 12 | temp1 = [-47.5, -10.4, 19.1, 25.9, 18.9, -10.4, -2.1, -47.6, 41.8, -12.1, -15.7, 12.1, -11.0, -0.6, -15.6, -7.6, 14.9, 43.5, 16.6, 0.1, 3.6, -33.5, -14.2, 20.8, 17.8, -29.8, -2.2, -12.8, 44.6, 19.7, 17.9, -41.3, 24.6, 37.0, 43.9, 14.5, 23.8, 19.6, -4.2, -40.5, 32.0, 17.2, 22.6, -26.9, 9.9, -33.4, -13.6, 6.6, 48.5, -3.5, -9.9, -39.9, -28.2, 20.7, 7.1, 15.5, -36.2, -29.9, -18.2, 11.1, -1.2, -13.7, 9.3, 9.3, 39.2, 15.8, -5.2, -16.2, -34.9, 5.0, -13.4, -31.8, 24.7, -29.1, 1.4, 24.0, -24.4, 18.0, 11.9, -29.1, 36.3, 18.6, 30.3, 38.4, 4.8, -20.5, -46.8, 12.1, -44.2, -6.0, -1.4, -39.7, -1.0, -13.7, 13.3, 23.6, 37.4, -7.0, -22.3, 37.8, 17.6, -3.3, 35.0, -9.1, -44.5, 13.1, -5.1, 19.7, -12.1, 1.7, -30.9, -1.9, -19.4, -15.0, 10.8, 31.9, 19.7, 3.1, 29.9, -16.6, 31.7, -26.8, 38.1, 30.2, 3.5, 25.1, -14.8, 19.6, 2.1, 29.0, -9.6, -32.9, 24.8, 4.9, -2.2, -24.7, -4.3, -37.4, -3.0, 37.4, -34.0, -21.2, -18.4, 34.6, 9.3, -45.2, -21.1, -10.3, -19.8, 29.1, 31.3, 37.7, 27.2, 19.3, -1.6, -45.6, 35.3, -23.5, -39.9, -19.8, -3.8, 40.6, -15.7, 12.5, -0.8, -16.3, -5.1, 13.1, -13.8, -25.7, 43.8, 5.6, 9.2, 38.6, 42.2, 0.2, -10.0, -48.6, 14.1, -6.5, 34.6, -26.8, 11.1, -6.7, -6.1, 25.1, -38.3, 8.1] 13 | temp2 = [-14.8, 10.9, 18.8, -0.1, -11.3, 5.7, -19.7, 6.9, -11.5, -16.7, -45.4, -15.3, 6.0, -46.9, -24.1, -26.3, 30.2, 27.4, 21.4, -27.2, 12.1, -36.1, 23.8, -38.7, 41.5, 5.3, -8.7, 25.5, 36.6, -5.9, 43.7, -14.6, -9.7, -8.6, 34.7, -19.3, -15.5, 19.3, 21.4, 3.9, 34.0, 29.8, 6.5, 19.5, 28.2, -21.7, 13.4, -41.8, -25.9, -6.9, 37.5, 27.8, 18.1, 44.7, -43.0, -19.9, -15.7, 18.0, 2.4, -31.6, 9.6, -37.6, 15.4, -28.8, 43.6, -11.2, 4.6, -10.2, -8.8, 38.2, 8.7, -34.6, -4.7, 14.1, -1.7, 31.3, 0.6, 27.9, 26.3, 13.7, -1.2, 26.3, 32.1, -17.7, 15.5, 32.6, -14.4, -12.6, 22.3, -22.5, 7.0, 48.5, -6.4, 20.5, -42.9, 4.2, -23.0, 31.6, -24.6, 14.0, -30.2, -26.5, -29.0, 15.7, 6.0, 36.3, 44.3, 13.5, -27.6, 33.7, 13.4, -43.9, 10.5, 28.9, 47.0, 1.4, 10.2, 14.0, 13.3, -15.9, -3.4, -25.6, -14.7, 10.5, 21.6, 27.6, 21.8, 10.6, -37.8, -14.2, 7.6, -21.8, -8.6, 1.3, 6.8, -13.3, 40.9, -15.3, -10.3, 41.1, 6.0, -10.8, -1.5, -31.4, -35.6, 1.0, 2.5, -14.3, 24.4, -2.6, -24.1, -35.3, -29.9, -34.7, 15.9, -1.0, 19.5, 7.0, 44.5, 19.1, 39.7, 2.7, 2.7, 42.4, -23.0, 25.9, 25.0, 28.2, 31.2, -32.8, 3.9, -38.4, -44.8, 2.7, -39.9, -19.3, -7.0, -0.6, 5.8, -10.9, -44.5, 19.9, -31.5, -1.2] 14 | 15 | ## uncomment this to take input from keyboard 16 | # temp1 = [] 17 | # temp2 = [] 18 | # n0 = int(input()) 19 | # xy0 = [] 20 | # for i in range(0, n0): 21 | # (x,y) = (float(x) for x in input().split()) 22 | # xy0.append([x,y]) 23 | # temp1.append(x) 24 | # temp1.append(y) 25 | # n1 = int(input()) 26 | # xy1 = [] 27 | # for i in range(0, n1): 28 | # (x,y) = (float(x) for x in input().split()) 29 | # xy1.append([x,y]) 30 | # temp2.append(x) 31 | # temp2.append(y) 32 | 33 | ##------------------------------------------------------------------------------------- 34 | class Dist: 35 | ix = None 36 | d = None 37 | def __init__(self): 38 | pass 39 | 40 | class Cluster: 41 | x = None 42 | y = None 43 | iy = None 44 | err = None 45 | ix = [] 46 | d = [] 47 | def __init__(self): 48 | pass 49 | 50 | max_r = 5.0 51 | max_err = 0.2 52 | max_rr = max_r**2 53 | max_errr = max_err**2 54 | ix0 = [None] * n0 55 | ix1 = [None] * n1 56 | wi0, wi1 = None, None 57 | cl0 = [] 58 | cl1 = [] 59 | txy1 = [[None for j in range(2)] for i in range(n1)] 60 | ##-------------------------------------------------------------------------------------- 61 | 62 | def atanxy(x, y): 63 | pi = math.pi 64 | pi2 = 2 * math.pi 65 | sx, sy, a = None, None, None 66 | _zero = 1.0e-30 67 | sx = 0; 68 | if x < -_zero: 69 | sx = -1 70 | if x > +_zero: 71 | sx = +1 72 | sy = 0 73 | if y < -_zero: 74 | sy = -1 75 | if y > +_zero: 76 | sy = +1 77 | if sy == 0 and sx == 0: 78 | return 0 79 | if sx == 0 and sy > 0: 80 | return 0.5 * pi 81 | if sx == 0 and sy < 0: 82 | return 1.5 * pi 83 | if sy == 0 and sx > 0: 84 | return 0 85 | if sy == 0 and sx < 0: 86 | return pi 87 | a = y/x 88 | if a < 0: 89 | a = -a 90 | a = math.atan(a) 91 | if x > 0 and y > 0: 92 | a = a 93 | if x < 0 and y > 0: 94 | a = pi-a 95 | if x < 0 and y < 0: 96 | a = pi + a 97 | if x > 0 and y < 0: 98 | a = pi2 - a 99 | return a 100 | 101 | def compute(): 102 | i0,i1,e,f = None, None, None, None 103 | a,x,y = None, None, None 104 | ## original indexes (to keep track) 105 | for e in range(0, n0): 106 | ix0[e] = e 107 | for e in range(0, n1): 108 | ix1[e] = e 109 | ## sort xy0[] by x asc 110 | e = 1 111 | while e: 112 | e = 0 113 | i0 = 0 114 | for i1 in range(1, n0): 115 | if xy0[i0][0] > xy0[i1][0]: 116 | e = ix0[i0] 117 | ix0[i0] = ix0[i1] 118 | ix0[i1] = e 119 | e = 1 120 | a = xy0[i0][0] 121 | xy0[i0][0] = xy0[i1][0] 122 | xy0[i1][0] = a 123 | a = xy0[i0][1] 124 | xy0[i0][1] = xy0[i1][1] 125 | xy0[i1][1] = a 126 | i0 += 1 127 | ## sort xy1[] by x asc 128 | e = 1 129 | while e: 130 | e = 0 131 | i0 = 0 132 | for i1 in range(1, n1): 133 | if xy1[i0][0] > xy1[i1][0]: 134 | e = ix1[i0] 135 | ix1[i0] = ix1[i1] 136 | ix1[i1] = e 137 | e = 1 138 | a = xy1[i0][0] 139 | xy1[i0][0] = xy1[i1][0] 140 | xy1[i1][0] = a 141 | a = xy1[i0][1] 142 | xy1[i0][1] = xy1[i1][1] 143 | xy1[i1][1] = a 144 | i0 += 1 145 | ##---------------------- 146 | d = Dist() 147 | c = Cluster() 148 | pc, pd = None, None 149 | dist = [] 150 | 151 | ## find star clusters in xy0[] 152 | cl0 = [] 153 | for i0 in range(0, n0): 154 | dist = [] 155 | for i1 in range(i0+1, n0): 156 | if abs(xy0[i0][0] - xy0[i1][0]) > max_r: 157 | break 158 | x = xy0[i0][0] - xy0[i1][0] 159 | x *= x 160 | y = xy0[i0][1] - xy0[i1][1] 161 | y *= y 162 | a = x + y 163 | 164 | if a <= max_rr: 165 | d.ix = i1 166 | d.d = a 167 | dist.append(copy.copy(d)) 168 | if len(dist) >= 2: 169 | c.ix = [] 170 | c.err = -1.0 171 | c.ix.append(copy.copy(i0)) 172 | for i1 in range(0, len(dist)): 173 | c.ix.append(copy.copy(dist[i1].ix)) 174 | c.iy = -1 #?????????????????????? 175 | c.x = xy0[i0][0] 176 | for i1 in range(0, len(dist)): 177 | c.x += xy0[dist[i1].ix][0] 178 | c.x /= len(dist) + 1 #??????????????????? 179 | c.y = xy0[i0][1] 180 | for i1 in range(0, len(dist)): 181 | c.y += xy0[dist[i1].ix][1] 182 | c.y /= len(dist) + 1 #??????????????????????? 183 | 184 | e = 1 185 | for i1 in range(0, len(cl0)): 186 | # pc = cl0[i1] 187 | x = c.x - cl0[i1].x 188 | x *= x 189 | y = c.y - cl0[i1].y 190 | y *= y 191 | a = x + y 192 | if a < max_rr: 193 | cl0[i1].x = 0.5 * (cl0[i1].x + c.x) 194 | cl0[i1].y = 0.5 * (cl0[i1].y + c.y) 195 | for e in range(0, len(c.ix)): 196 | for f in range(0, len(cl0[i1].ix)): 197 | if cl0[i1].ix[f] == c.ix[e]: 198 | f = -1 199 | break 200 | if f >= 0: 201 | cl0[i1].ix.append(copy.copy(c.ix[e])) 202 | e = 0 203 | break 204 | if e: 205 | cl0.append(copy.copy(c)) 206 | ## full recompute clusters 207 | pc = cl0 208 | for f in range(0, len(cl0)): 209 | pc[f].x = 0.0 210 | for i1 in range(0, len(pc[f].ix)): 211 | pc[f].x += xy0[pc[f].ix[i1]][0] 212 | pc[f].x /= len(pc[f].ix) #?????????????? 213 | pc[f].y = 0.0 214 | for i1 in range(0, len(pc[f].ix)): 215 | pc[f].y += xy0[pc[f].ix[i1]][1] 216 | pc[f].y /= len(pc[f].ix) #?????????????? 217 | 218 | ## distances 219 | pc[f].d = [] 220 | for i0 in range(0, len(pc[f].ix)): 221 | for i1 in range(i0+1, len(pc[f].ix)): 222 | x = xy0[pc[f].ix[i1]][0] - xy0[pc[f].ix[i0]][0] 223 | x *= x 224 | y = xy0[pc[f].ix[i1]][1] - xy0[pc[f].ix[i0]][1] 225 | y *= y 226 | pc[f].d.append(copy.copy(math.sqrt(x + y))) 227 | ## sort by distance asc 228 | e = 1 229 | while e: 230 | e = 0 231 | i0 = 0 232 | for i1 in range(1, len(pc[f].d)): 233 | if pc[f].d[i0] > pc[f].d[i1]: 234 | a = pc[f].d[i0] 235 | pc[f].d[i0] = pc[f].d[i1] 236 | pc[f].d[i1] = a 237 | e = 1 238 | i0 += 1 239 | ## find star clusters in xy1[] 240 | cl1 = [] 241 | for i0 in range(0, n1): 242 | dist = [] 243 | for i1 in range(i0+1, n1): 244 | if abs(xy1[i0][0] - xy1[i1][0]) > max_r: 245 | break 246 | x = xy1[i0][0] - xy1[i1][0] 247 | x *= x 248 | y = xy1[i0][1] - xy1[i1][1] 249 | y *= y 250 | a = x + y 251 | 252 | if a <= max_rr: 253 | d.ix = i1 254 | d.d = a 255 | dist.append(copy.copy(d)) 256 | if len(dist) >= 2: 257 | c.ix = [] 258 | c.err = -1.0 259 | c.ix.append(copy.copy(i0)) 260 | for i1 in range(0, len(dist)): 261 | c.ix.append(copy.copy(dist[i1].ix)) 262 | c.iy = -1 # ??????????????????? 263 | c.x = xy1[i0][0] 264 | for i1 in range(0, len(dist)): 265 | c.x += xy1[dist[i1].ix][0] 266 | c.x /= len(dist) + 1 # ?????????????? 267 | c.y = xy1[i0][1] 268 | for i1 in range(0, len(dist)): 269 | c.y += xy1[dist[i1].ix][1] 270 | c.y /= len(dist) + 1 # ?????????????? 271 | 272 | e = 1 273 | for i1 in range(0, len(cl1)): 274 | pc = cl1[i1] 275 | x = c.x - pc.x 276 | x *= x 277 | y = c.y - pc.y 278 | y *= y 279 | a = x + y 280 | if a < max_rr: 281 | pc.x = 0.5 * (pc.x + c.x) 282 | pc.y = 0.5 * (pc.y + c.y) 283 | for e in range(0, len(c.ix)): 284 | for f in range(0, len(pc.ix)): 285 | if pc.ix[f] == c.ix[e]: 286 | f = -1 287 | break 288 | if f >= 0: 289 | pc.ix.append(copy.copy(c.ix[e])) 290 | e = 0 291 | break 292 | if e: 293 | cl1.append(copy.copy(c)) 294 | ## full recompute clusters 295 | pc = cl1 296 | for f in range(0, len(cl1)): 297 | pc[f].x = 0.0 298 | for i1 in range(0, len(pc[f].ix)): 299 | pc[f].x += xy1[pc[f].ix[i1]][0] 300 | pc[f].x /= len(pc[f].ix) #????????????? 301 | pc[f].y = 0.0 302 | for i1 in range(0, len(pc[f].ix)): 303 | pc[f].y += xy1[pc[f].ix[i1]][1] 304 | pc[f].y /= len(pc[f].ix) # ????????????? 305 | 306 | ## distances 307 | pc[f].d = [] 308 | for i0 in range(0, len(pc[f].ix)): 309 | for i1 in range(i0+1, len(pc[f].ix)): 310 | x = xy1[pc[f].ix[i1]][0] - xy1[pc[f].ix[i0]][0] 311 | x *= x 312 | y = xy1[pc[f].ix[i1]][1] - xy1[pc[f].ix[i0]][1] 313 | y *= y 314 | pc[f].d.append(math.sqrt(x + y)) 315 | ## sort by distance asc 316 | e = 1 317 | while e: 318 | e = 0 319 | i0 = 0 320 | for i1 in range(1, len(pc[f].d)): 321 | if pc[f].d[i0] > pc[f].d[i1]: 322 | a = pc[f].d[i0] 323 | pc[f].d[i0] = pc[f].d[i1] 324 | pc[f].d[i1] = a 325 | e = 1 326 | 327 | i0 += 1 328 | ## find matches 329 | pc = cl0 330 | for i0 in range(0,len(cl0)): 331 | if pc[i0].iy < 0: 332 | e = -1 333 | x = 0.0 334 | pd = cl1 335 | for i1 in range(0, len(cl1)): 336 | if len(pc[i0].d) == len(pd[i1].d): 337 | y = 0.0 338 | for f in range(0, len(pc[i0].d)): 339 | y += abs(pc[i0].d[f] - pd[i1].d[f]) 340 | if e < 0 or x > y: 341 | e = i1 342 | x = y 343 | x /= len(pc[i0].d) 344 | if e >= 0 and x < max_err: 345 | if cl1[e].iy >= 0: 346 | cl0[cl1[e].iy].iy = -1 347 | pc[i0].iy = e 348 | cl1[e].iy = i0 349 | pc[i0].err = x 350 | cl1[e].err = x 351 | 352 | ## compute transform 353 | tx0 = 0.0 354 | tx1 = 0.0 355 | ty0 = 0.0 356 | ty1 = 0.0 357 | tc = 1.0 358 | ts = 0.0 359 | i0 = -1 360 | i1 = -1 361 | 362 | pc = cl0 363 | f = 0 364 | for e in range(0, len(cl0)): 365 | if pc[e].iy >= 0: 366 | if f == 0: 367 | i0 = e 368 | if f == 1: 369 | i1 = e 370 | break 371 | f += 1 372 | if (i1 >= 0): 373 | pc = cl0[i0] 374 | pd = cl0[i1] 375 | tx1 = pc.x 376 | ty1 = pc.y 377 | a = atanxy(pd.x - pc.x, pd.y - pc.y) 378 | pc = cl1[pc.iy] 379 | pd = cl1[pd.iy] 380 | tx0 = pc.x 381 | ty0 = pc.y 382 | a -= atanxy(pd.x - pc.x, pd.y - pc.y) 383 | tc = math.cos(a) 384 | ts = math.sin(a) 385 | ## transform xy1 -. txy1 (in xy0 coordinate system 386 | for i1 in range(0, n1): 387 | x = xy1[i1][0] - tx0 388 | y = xy1[i1][1] - ty0 389 | txy1[i1][0] = x * tc - y * ts + tx1 390 | txy1[i1][1] = x * ts + y * tc + ty1 391 | 392 | ## sort txy1[] by x asc (after transfrm) 393 | e = 1 394 | while e: 395 | e = 0 396 | i0 = 0 397 | for i1 in range(1, n1): 398 | if txy1[i0][0] > txy1[i1][0]: 399 | e = ix1[i0] 400 | ix1[i0] = ix1[i1] 401 | ix1[i1] = e 402 | e = 1 403 | a = txy1[i0][0] 404 | txy1[i0][0] = txy1[i1][0] 405 | txy1[i1][0] = a 406 | a = txy1[i0][1] 407 | txy1[i0][1] = txy1[i1][1] 408 | txy1[i1][1] = a 409 | i0 += 1 410 | ## find match between xy0,txy1 (this can be speeded up by exploiting sorted order) 411 | ix01 = [0 for i in range(n0)] 412 | ix10 = [0 for i in range(n1)] 413 | for i0 in range(0, n0): 414 | ix01[i0] = -1 415 | for i1 in range(0, n1): 416 | ix10[i1] = -1 417 | for i0 in range(0, n0): 418 | a = -1.0 419 | for i1 in range(0, n1): 420 | x = xy0[i0][0] - txy1[i1][0] 421 | x *= x 422 | y = xy0[i0][1] - txy1[i1][1] 423 | y *= y 424 | x += y 425 | if x < max_errr: 426 | if a < 0.0 or a > x: 427 | a = x 428 | ix01[i0] = i1 429 | ix10[i1] = i0 430 | ## find the closest stars from unmatched stars 431 | a = -1.0 432 | wi0 = -1 433 | wi1 = -1 434 | for i0 in range(0, n0): 435 | if ix01[i0] < 0: 436 | for i1 in range(0, n1): 437 | if ix10[i1] < 0: 438 | x = xy0[i0][0] - txy1[i1][0] 439 | x *= x 440 | y = xy0[i0][1] - txy1[i1][1] 441 | y *= y 442 | x += y 443 | if wi0 < 0 or a > x: 444 | a = x 445 | wi0 = i0 446 | wi1 = i1 447 | 448 | # print(xy0[wi0][0], xy0[wi0][1]) 449 | # print(txy1[wi1][0], txy1[wi1][1]) 450 | 451 | print(ix0[wi0], ix1[wi1]) 452 | 453 | compute() -------------------------------------------------------------------------------- /WanderingStarBroken.py: -------------------------------------------------------------------------------- 1 | #input 2 | # http://www.shodor.org/interactivate/activities/SimplePlot/ 3 | # http://en.wikipedia.org/wiki/Rotation_matrix 4 | # http://reference.wolfram.com/language/ref/RotationMatrix.html 5 | # http://www.wolframalpha.com/input/?i=RotationMatrix%5B90+Degree%5D 6 | # http://www.wolframalpha.com/input/?i=%7B%7B0%2C+-1%7D%2C+%7B1%2C+0%7D%7D+*+%7B1%2C+1%7D 7 | # http://nghiaho.com/?page_id=671 8 | # http://math.stackexchange.com/questions/180418/calculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d 9 | # after you find the rotation matrix, multiply each vector with the matrix 10 | # calculate the rotation matrix by centroids ? 11 | # dot product gives angle 12 | # cross product gives length?TODO TODO cross product a1a2 b1b2 are actually lengths not points? 13 | # a 8.994117647058818 2.423529411764707 14 | # b 7.9529411764705875 -0.6049019607843146 15 | # |a x b| = a1*b2 - b1*a2 = |-24.7147462514417647991234140715053| 16 | # a . b = ||a||||b|| cos 0 = a1*b1 + a2*b2 = 70.0636908881199004800230680507528 17 | # if a . b = 0 -> theta = pi/2 18 | # if a . b > 0 -> theta < pi/2 19 | # if a . b < 0 -> theta > pi/2 20 | 21 | def normalize(v): 22 | length = (v[0]**2 + v[1]**2)**.5 23 | normalized_v = (v[0]/length, v[1]/length) 24 | return normalized_v 25 | 26 | def cross_product(a, b): 27 | cp = a[0] * b[1] - b[0] * a[1] 28 | return abs(cp) 29 | 30 | def dot_product(a, b): 31 | dp = a[0] * b[0] + a[1] * b[1] 32 | return dp 33 | 34 | def rotation_matrix(a, b): 35 | dp = dot_product(a,b) 36 | cp = cross_product(a,b) 37 | 38 | #(dp, -cp),(cp,dp) ???? 39 | # R = (normalize((dp, cp)), normalize((-cp, dp))) ##TODO good 40 | R = (normalize((dp, cp)), normalize((-cp, dp))) 41 | 42 | return R 43 | 44 | def multiply_mv(m, v): 45 | result = [0,0] 46 | for i in range(0, 2): 47 | for j in range(0, 2): 48 | result[i] += m[i][j] * v[j] 49 | 50 | return tuple(result) 51 | 52 | def subtract_vectors(a, b): 53 | result = (b[0] - a[0], b[1] - a[1]) 54 | return result 55 | 56 | def add_vectors(a, b): 57 | result = (a[0] + b[0], a[1] + b[1]) 58 | return result 59 | 60 | def transform_vector(v, a, b): 61 | R = rotation_matrix(a, b) 62 | # print(R) 63 | mv = multiply_mv(R, a) 64 | t = subtract_vectors(mv, b) 65 | 66 | new_mv = multiply_mv(R, v) 67 | new_v = add_vectors(new_mv, t) 68 | 69 | return new_v 70 | 71 | def transform_vector_degree(v, degree): 72 | R = rotation_matrix_degree(degree) 73 | mv = multiply_mv(R, v) 74 | return mv 75 | 76 | def calculate_distance(arrays): 77 | arrays_lengths = (len(arrays[0]), len(arrays[1])) 78 | index = -1 79 | if arrays_lengths[0] < arrays_lengths[1]: 80 | index = 0 81 | else: 82 | index = 1 83 | distance = 0 84 | for i in range(0, len(arrays[index])): 85 | # lengtha = dist_to_origin(arrays[0][i]) 86 | # lengthb = dist_to_origin(arrays[1][i]) 87 | # if lengtha > 30 or lengthb > 30: 88 | # continue 89 | distance += ((arrays[1][i][0] - arrays[0][i][0])**2 + (arrays[1][i][1] - arrays[0][i][1])**2)**.5 90 | return distance 91 | 92 | def calculate_distance_points(a, b): 93 | dist = ((b[0] - a[0])**2 + (b[1] - a[1])**2)**.5 94 | return dist 95 | 96 | def rotation_matrix_degree(degree): 97 | # rad = (180 * degree) / math.pi 98 | rad = degree * math.pi/180 99 | if degree > 0: 100 | R = (normalize((math.cos(rad), -math.sin(rad))), normalize((math.sin(rad), math.cos(rad)))) 101 | else: 102 | R = (normalize((math.cos(rad), math.sin(rad))), normalize((-math.sin(rad), math.cos(rad)))) 103 | return R 104 | 105 | def find_centroid(a): 106 | averagex = 0 107 | averagey = 0 108 | for i in range(0, len(a)): 109 | averagex += a[i][0] 110 | averagey += a[i][1] 111 | averagex /= len(a) 112 | averagey /= len(a) 113 | return (averagex, averagey) 114 | 115 | # a = (5.4,38.8) 116 | # b = (28.3,28.8) 117 | # a = (8.994117647058818, 2.423529411764707) 118 | # b = (7.9529411764705875, -0.6049019607843146) 119 | # v = (6.00, 48.00) 120 | # print(transform_vector(v, a, b)) 121 | 122 | import math 123 | import operator 124 | import random 125 | 126 | class Problem: 127 | __lengtha = 0 128 | __lengthb = 0 129 | __initial_vectora = [] 130 | __initial_vectorb = [] 131 | 132 | def getInitialVectorA(self): 133 | return self.__initial_vectora 134 | def setInitialVectorA(self,a): 135 | self.__initial_vectora = a 136 | self.__lengtha = len(self.__initial_vectora) 137 | def getInitialVectorB(self): 138 | return self.__initial_vectorb 139 | def setInitialVectorB(self,b): 140 | self.__initial_vectorb = b 141 | self.__lengthb = len(self.__initial_vectorb) 142 | def getLengthA(self): 143 | return self.__lengtha 144 | def getLengthB(self): 145 | return self.__lengthb 146 | 147 | # n = [] 148 | # sections = [] 149 | 150 | problem = Problem() 151 | problem.setInitialVectorA([(-10.1, -20.8), (-16.8, -12.5), (1.0, 26.8), (11.5, 44.7), (-3.8, -47.5), (36.7, -23.1), (-9.5, 19.5), (13.0, -42.1), (-9.9, 42.2), (16.0, 35.5), (-2.2, 47.0), (16.6, -46.3), (1.2, 26.0), (-28.3, -35.3), (-13.7, 42.8), (-28.7, -3.1), (-11.3, 13.3), (33.2, 21.8), (-16.4, 0.4), (8.1, -11.5), (31.3, -34.6), (46.4, 3.5), (5.4, 38.8), (-33.2, -26.2), (-44.6, 15.0), (-6.8, 46.2), (-19.0, 43.7), (9.9, 9.9), (34.5, 8.1), (-14.2, 21.5), (8.3, 42.3), (16.4, 21.7), (9.7, -47.7), (38.7, -25.8), (43.6, 4.1), (22.5, -42.9), (20.6, -32.6), (29.6, -24.3), (12.1, 44.9), (4.3, 27.6), (41.8, 8.2), (-3.0, 42.0), (38.0, -28.2), (41.4, 19.9), (-3.9, 49.5), (-11.5, -22.2), (38.0, -27.8), (30.4, -21.5), (15.5, -5.6), (-42.5, -4.2), (26.7, -16.2), (28.9, -31.5), (27.5, 25.5), (35.6, 29.1), (-0.5, -46.7), (0.9, -39.2), (-9.5, -14.1), (31.5, -26.8), (28.5, 33.8), (-3.5, 45.3), (-28.5, 0.3), (24.5, 40.0), (26.0, 32.2), (27.5, 15.2), (-2.2, 30.8), (16.3, -6.8), (11.1, -39.5), (31.1, -15.0), (8.0, -17.9), (33.3, -31.9), (-9.9, 41.9), (18.0, -23.3), (37.1, 17.0), (6.2, -1.6), (9.6, -9.6), (36.0, 4.8), (-15.5, -23.4), (36.9, -18.2), (19.9, 13.4), (-11.1, 23.8), (35.1, 7.8), (27.9, -9.4), (12.8, 2.9), (9.4, 9.7), (-38.3, 25.8), (16.4, 1.1), (30.3, 17.1), (11.0, 5.7), (-17.9, 39.2), (-25.7, -24.1), (14.4, 17.9), (26.6, -12.7), (-0.8, 16.1), (31.4, -17.4), (-0.7, 29.3), (-44.1, 7.1), (31.4, 18.5), (10.3, -36.9), (-20.3, 23.4), (17.3, -34.9), (43.8, 17.6), (-39.6, -29.1)]) 152 | problem.setInitialVectorB([(9.0, -6.8), (37.1, 30.8), (-5.1, -45.3), (20.2, -36.0), (12.1, -15.9), (14.5, -47.8), (16.7, 39.3), (-23.2, 40.4), (44.8, -15.8), (21.2, -26.5), (-46.5, -9.0), (-14.2, -36.8), (-19.1, -42.6), (16.8, -2.8), (23.1, -32.9), (-9.7, 6.2), (1.2, 14.8), (45.9, 13.0), (18.3, 23.5), (24.9, -40.5), (-4.9, -39.5), (6.2, -40.8), (-37.6, -19.0), (29.4, 8.5), (40.8, -1.7), (-43.7, 20.0), (-16.6, -15.9), (8.5, -31.5), (-20.6, -15.0), (27.5, -36.4), (45.5, 20.4), (-5.5, -42.1), (-27.0, 32.8), (-45.5, -6.9), (25.7, 39.6), (36.5, 30.9), (42.9, 12.8), (17.7, 25.6), (8.2, 40.6), (-1.7, 28.1), (14.0, -44.0), (6.5, 23.9), (16.5, 39.0), (19.2, -9.4), (38.8, -12.9), (18.5, -41.2), (21.8, 19.6), (-22.1, 9.2), (39.2, -2.4), (17.0, -6.1), (28.3, -0.5), (38.4, -12.3), (11.7, 12.1), (38.1, -15.9), (-22.4, -41.7), (35.9, 20.7), (-16.1, 39.1), (-10.8, -9.9), (8.0, -15.4), (23.7, 38.8), (17.4, 1.5), (2.5, -21.8), (2.2, -41.2), (-34.6, 15.1), (-37.4, -8.7), (18.5, 20.5), (-10.7, -48.6), (18.1, -38.1), (32.5, 30.4), (22.6, 35.7), (44.3, -20.3), (2.7, 23.4), (28.3, 28.8), (-14.7, -15.4), (-5.5, 47.9), (-46.0, 0.0), (15.2, -16.3), (-22.0, 43.5), (24.0, -30.7), (-30.4, 25.7), (25.5, 42.7), (45.1, -5.9), (17.9, 1.4), (9.5, 45.1), (-18.5, 44.0), (21.3, 41.2), (5.8, -16.3), (-16.4, -4.9), (40.9, 6.3), (-29.9, -10.5), (17.6, -46.5), (44.0, 0.2), (35.8, -2.7), (-44.4, -8.0), (5.8, 19.3), (25.8, 6.1), (-34.9, -34.6), (-20.2, 12.0), (13.6, 41.7), (24.0, -24.3), (19.6, -29.6), (18.3, 19.7)]) 153 | 154 | # n = [102,102] 155 | # sections = [[(-10.1, -20.8), (-16.8, -12.5), (1.0, 26.8), (11.5, 44.7), (-3.8, -47.5), (36.7, -23.1), (-9.5, 19.5), (13.0, -42.1), (-9.9, 42.2), (16.0, 35.5), (-2.2, 47.0), (16.6, -46.3), (1.2, 26.0), (-28.3, -35.3), (-13.7, 42.8), (-28.7, -3.1), (-11.3, 13.3), (33.2, 21.8), (-16.4, 0.4), (8.1, -11.5), (31.3, -34.6), (46.4, 3.5), (5.4, 38.8), (-33.2, -26.2), (-44.6, 15.0), (-6.8, 46.2), (-19.0, 43.7), (9.9, 9.9), (34.5, 8.1), (-14.2, 21.5), (8.3, 42.3), (16.4, 21.7), (9.7, -47.7), (38.7, -25.8), (43.6, 4.1), (22.5, -42.9), (20.6, -32.6), (29.6, -24.3), (12.1, 44.9), (4.3, 27.6), (41.8, 8.2), (-3.0, 42.0), (38.0, -28.2), (41.4, 19.9), (-3.9, 49.5), (-11.5, -22.2), (38.0, -27.8), (30.4, -21.5), (15.5, -5.6), (-42.5, -4.2), (26.7, -16.2), (28.9, -31.5), (27.5, 25.5), (35.6, 29.1), (-0.5, -46.7), (0.9, -39.2), (-9.5, -14.1), (31.5, -26.8), (28.5, 33.8), (-3.5, 45.3), (-28.5, 0.3), (24.5, 40.0), (26.0, 32.2), (27.5, 15.2), (-2.2, 30.8), (16.3, -6.8), (11.1, -39.5), (31.1, -15.0), (8.0, -17.9), (33.3, -31.9), (-9.9, 41.9), (18.0, -23.3), (37.1, 17.0), (6.2, -1.6), (9.6, -9.6), (36.0, 4.8), (-15.5, -23.4), (36.9, -18.2), (19.9, 13.4), (-11.1, 23.8), (35.1, 7.8), (27.9, -9.4), (12.8, 2.9), (9.4, 9.7), (-38.3, 25.8), (16.4, 1.1), (30.3, 17.1), (11.0, 5.7), (-17.9, 39.2), (-25.7, -24.1), (14.4, 17.9), (26.6, -12.7), (-0.8, 16.1), (31.4, -17.4), (-0.7, 29.3), (-44.1, 7.1), (31.4, 18.5), (10.3, -36.9), (-20.3, 23.4), (17.3, -34.9), (43.8, 17.6), (-39.6, -29.1)],[(9.0, -6.8), (37.1, 30.8), (-5.1, -45.3), (20.2, -36.0), (12.1, -15.9), (14.5, -47.8), (16.7, 39.3), (-23.2, 40.4), (44.8, -15.8), (21.2, -26.5), (-46.5, -9.0), (-14.2, -36.8), (-19.1, -42.6), (16.8, -2.8), (23.1, -32.9), (-9.7, 6.2), (1.2, 14.8), (45.9, 13.0), (18.3, 23.5), (24.9, -40.5), (-4.9, -39.5), (6.2, -40.8), (-37.6, -19.0), (29.4, 8.5), (40.8, -1.7), (-43.7, 20.0), (-16.6, -15.9), (8.5, -31.5), (-20.6, -15.0), (27.5, -36.4), (45.5, 20.4), (-5.5, -42.1), (-27.0, 32.8), (-45.5, -6.9), (25.7, 39.6), (36.5, 30.9), (42.9, 12.8), (17.7, 25.6), (8.2, 40.6), (-1.7, 28.1), (14.0, -44.0), (6.5, 23.9), (16.5, 39.0), (19.2, -9.4), (38.8, -12.9), (18.5, -41.2), (21.8, 19.6), (-22.1, 9.2), (39.2, -2.4), (17.0, -6.1), (28.3, -0.5), (38.4, -12.3), (11.7, 12.1), (38.1, -15.9), (-22.4, -41.7), (35.9, 20.7), (-16.1, 39.1), (-10.8, -9.9), (8.0, -15.4), (23.7, 38.8), (17.4, 1.5), (2.5, -21.8), (2.2, -41.2), (-34.6, 15.1), (-37.4, -8.7), (18.5, 20.5), (-10.7, -48.6), (18.1, -38.1), (32.5, 30.4), (22.6, 35.7), (44.3, -20.3), (2.7, 23.4), (28.3, 28.8), (-14.7, -15.4), (-5.5, 47.9), (-46.0, 0.0), (15.2, -16.3), (-22.0, 43.5), (24.0, -30.7), (-30.4, 25.7), (25.5, 42.7), (45.1, -5.9), (17.9, 1.4), (9.5, 45.1), (-18.5, 44.0), (21.3, 41.2), (5.8, -16.3), (-16.4, -4.9), (40.9, 6.3), (-29.9, -10.5), (17.6, -46.5), (44.0, 0.2), (35.8, -2.7), (-44.4, -8.0), (5.8, 19.3), (25.8, 6.1), (-34.9, -34.6), (-20.2, 12.0), (13.6, 41.7), (24.0, -24.3), (19.6, -29.6), (18.3, 19.7)]] 156 | 157 | # for i in range(0, 2): 158 | # maxj = len(sections[i]) 159 | # j = 0 160 | # while j < maxj: 161 | # dist = (sections[i][j][0]**2 + sections[i][j][1]**2)**.5 162 | # if dist > 30: 163 | # del sections[i][j] 164 | # j -= 1 165 | # maxj -= 1 166 | # j += 1 167 | # 168 | # print(sections[0]) 169 | # print(sections[1]) 170 | 171 | ############################### 172 | # for i in range(0, 2): 173 | # n.append(int(input())) 174 | # 175 | # section = [] 176 | # for j in range(0, n[i]): 177 | # (x, y) = (float(x) for x in input().split()) 178 | # # since some points on the edge might be absent, 179 | # # ignore all points that have a length > 40 180 | # # if ((x**2 + y**2)**.5 <= 40): 181 | # section.append((x,y)) 182 | # 183 | # sections.append(section) 184 | ################################ 185 | 186 | 187 | # print(len(sections[0]), len(sections[1])) 188 | 189 | 190 | ################################################################ 191 | ## distance minimization 192 | # calculate sections centroids 193 | # averages = [] 194 | # for i in range(0, 2): 195 | # 196 | # averagex = 0 197 | # averagey = 0 198 | # 199 | # for j in range(0, n[i]): 200 | # averagex += sections[i][j][0] 201 | # averagey += sections[i][j][1] 202 | # 203 | # averagex /= len(sections[i]) 204 | # averagey /= len(sections[i]) 205 | # 206 | # averages.append((averagex, averagey)) 207 | # # print(averagex, averagey) 208 | # 209 | # # #align the centroids 210 | # R = rotation_matrix(averages[0], averages[1]) 211 | # rotated_vectors = [] 212 | # for i in range(0, n[0]): 213 | # rotated_vectors.append(transform_vector(sections[0][i], averages[0], averages[1])) 214 | # 215 | # 216 | # #distance minimization 217 | # distances = [] 218 | # temp_vector = rotated_vectors#sections[0]#rotated_vectors 219 | # for i in range(1, 360): 220 | # rotated_vector = [] 221 | # for j in range(0, n[0]): 222 | # rotated_vector.append(transform_vector_degree(temp_vector[j], i)) 223 | # 224 | # distance = calculate_distance((rotated_vector, sections[1])) 225 | # 226 | # distances.append((i,distance,rotated_vector)) 227 | # 228 | # temp_vector = rotated_vector 229 | # 230 | # 231 | # 232 | # distances = sorted(distances, key=operator.itemgetter(1)) 233 | # 234 | # print(distances[0][2]) 235 | # for i in range(0, 20): 236 | # toString = (str(distances[i][2]).replace('[','(')).replace(']',')') 237 | # print(toString) 238 | # # print(distances[i][0], distances[i][2]) 239 | ###################################################################### 240 | 241 | 242 | ##################################################################### 243 | #search for shape 244 | # sort the point by distance from origin 245 | def dist_to_origin(a): 246 | dist = (a[0]**2 + a[1]**2)**.5 247 | return dist 248 | 249 | def find_nearest_neighbors(p, array, num_of_neigh): 250 | # [[distance, to_point]] 251 | dist_points = [] 252 | for i in range(0, len(array)): 253 | dist = calculate_distance_points(p, array[i]) 254 | #if dist is 0 it's the same point, so ignore it 255 | if (dist == 0): 256 | continue 257 | list_entry = [dist, array[i]] 258 | dist_points.append(list_entry) 259 | 260 | #sort by distance 261 | dist_points = sorted(dist_points, key=operator.itemgetter(0)) 262 | neighbors = dist_points[:10] 263 | # print('dist_points', neighbors) 264 | return neighbors 265 | 266 | def has_similar_neigh(neigha, neighb, tolerance=0.2, min_matches=10): 267 | matches = 0 268 | for i in range(0, len(neigha)): 269 | for j in range(0, len(neighb)): 270 | # if neighb-telorance <= neigha <= neighb+tolerance 271 | if neighb[j][0]-tolerance <= neigha[i][0] and neigha[i][0] <= neighb[j][0]+tolerance: 272 | matches += 1 273 | if matches >= min_matches: 274 | return (True, matches) 275 | return (False, 0) 276 | 277 | sorted_by_dist_origina = sorted(problem.getInitialVectorA(), key=dist_to_origin) 278 | sorted_by_dist_originb = sorted(problem.getInitialVectorB(), key=dist_to_origin) 279 | sorted_arrays = [sorted_by_dist_origina, sorted_by_dist_originb] 280 | 281 | def already_matched(point, matched_points): 282 | for m in matched_points: 283 | if point == m.getPointInA(): 284 | return True 285 | return False 286 | 287 | class Point: 288 | __x = None 289 | __y = None 290 | def setX(self,x): 291 | self.__x = x 292 | def setY(self,y): 293 | self.__y = y 294 | def setPoint(self,x): 295 | self.setX(x[0]) 296 | self.setY(x[1]) 297 | def getPoint(self): 298 | return (self.getX(), self.getY()) 299 | def getX(self): 300 | return self.__x 301 | def getY(self): 302 | return self.__y 303 | def __init__(self,x,y): 304 | self.setX(x) 305 | self.setY(y) 306 | 307 | class Match: 308 | __pointInA = None 309 | __pointInB = None 310 | __numOfMatches = 0 311 | def setPointInA(self, a): 312 | self.__pointInA = Point(a[0], a[1]) 313 | def setPointInB(self, b): 314 | self.__pointInB = Point(b[0], b[1]) 315 | def setNumOfMatches(self, n): 316 | self.__numOfMatches = n 317 | def getPointInA(self): 318 | return self.__pointInA 319 | def getPointInB(self): 320 | return self.__pointInB 321 | def getNumOfMatches(self): 322 | return self.__numOfMatches 323 | def __init__(self, a, b, numOfMatches): 324 | self.setPointInA(a) 325 | self.setPointInB(b) 326 | self.setNumOfMatches(numOfMatches) 327 | 328 | matched_points = [] 329 | while len(matched_points) < 5: 330 | # chose the closest point to the origin as the observed point 331 | # then find n closest neighbors 332 | observed_point = sorted_by_dist_origina[random.randint(0, len(sorted_by_dist_origina) - 1)] 333 | dist = dist_to_origin(observed_point) 334 | if dist > 30: 335 | continue 336 | while already_matched(observed_point, matched_points): 337 | observed_point = sorted_by_dist_origina[random.randint(0, len(sorted_by_dist_origina) - 1)] 338 | observed_point_neigh = find_nearest_neighbors(observed_point, sorted_by_dist_origina, 10) 339 | 340 | # now search for points in the other array with the same pattern 341 | matches = [] 342 | for i in range(0, problem.getLengthB()): 343 | point = problem.getInitialVectorB()[i] 344 | point_neigh = find_nearest_neighbors(point, problem.getInitialVectorB(), 20) 345 | 346 | (is_match, number_of_matches) = has_similar_neigh(observed_point_neigh, point_neigh, 0.2, 10) 347 | if is_match: 348 | mat = Match(observed_point, point, number_of_matches) 349 | matches.append(mat) 350 | # 351 | 352 | if not matches: 353 | continue 354 | 355 | best_match = sorted(matches, key=lambda x: x.getNumOfMatches()) 356 | matched_points.append(best_match[0]) 357 | 358 | 359 | clustera = [None] * len(matched_points) 360 | clusterb = [None] * len(matched_points) 361 | for i in range(0, len(matched_points)): 362 | clustera[i] = matched_points[i].getPointInA().getPoint() 363 | clusterb[i] = matched_points[i].getPointInB().getPoint() 364 | centa = find_centroid(clustera) 365 | centb = find_centroid(clusterb) 366 | 367 | ## the first match should be the right one 368 | ## now calculate the rotation matrix and rotate the vectors 369 | # rotated_vectors = [] 370 | # for v in sorted_by_dist_origina: 371 | # x = transform_vector(v, centa, centb) 372 | # rotated_vectors.append(x) 373 | 374 | for m in matched_points: 375 | x = transform_vector(m.getPointInA().getPoint(), centa, centb) 376 | m.setPointInA(x) 377 | 378 | matched_points_in_a = [] 379 | matched_points_in_b = [] 380 | for u in range(0, len(matched_points)): 381 | matched_points_in_a.append(matched_points[u].getPointInA().getPoint()) 382 | matched_points_in_b.append(matched_points[u].getPointInB().getPoint()) 383 | 384 | dist = calculate_distance((matched_points_in_a, matched_points_in_b)) 385 | print(dist) 386 | 387 | # ## the rotation and alignment might be a bit off 388 | # ## we'll brute force by moving it along the axis and then rotate 389 | # ## until it's in the right place 390 | distance = dist 391 | moved_dist = distance 392 | moved_vectors = matched_points_in_a 393 | j = 0 394 | # while j < 1000: 395 | while moved_dist > 2: 396 | moving = True 397 | while moving: 398 | moving = False 399 | while True: 400 | ## move right 401 | new_v = [] 402 | for v in moved_vectors: 403 | new_v.append(add_vectors(v, (.5,0))) 404 | new_dist = calculate_distance((new_v, matched_points_in_b)) 405 | if new_dist < moved_dist: 406 | moved_vectors = new_v 407 | moved_dist = new_dist 408 | moving = True 409 | else: 410 | break 411 | while True: 412 | ## move up 413 | new_v = [] 414 | for v in moved_vectors: 415 | new_v.append(add_vectors(v, (0,.5))) 416 | new_dist = calculate_distance((new_v, matched_points_in_b)) 417 | if new_dist < moved_dist: 418 | moved_vectors = new_v 419 | moved_dist = new_dist 420 | moving = True 421 | else: 422 | break 423 | while True: 424 | ## move left 425 | new_v = [] 426 | for v in moved_vectors: 427 | new_v.append(add_vectors(v, (-.5,0))) 428 | new_dist = calculate_distance((new_v, matched_points_in_b)) 429 | if new_dist < moved_dist: 430 | moved_vectors = new_v 431 | moved_dist = new_dist 432 | moving = True 433 | else: 434 | break 435 | while True: 436 | ## move down 437 | new_v = [] 438 | for v in moved_vectors: 439 | new_v.append(add_vectors(v, (0,-.5))) 440 | new_dist = calculate_distance((new_v, matched_points_in_b)) 441 | if new_dist < moved_dist: 442 | moved_vectors = new_v 443 | moved_dist = new_dist 444 | moving = True 445 | else: 446 | break 447 | 448 | ## rotate 449 | k = 0 450 | while k < 1: 451 | k = 1 452 | ## rotate clockwise 453 | new_v = [] 454 | for v in moved_vectors: 455 | new_v.append(transform_vector_degree(v, .01)) 456 | new_dist = calculate_distance((new_v, matched_points_in_b)) 457 | if new_dist < moved_dist: 458 | moved_vectors = new_v 459 | moved_dist = new_dist 460 | else: 461 | break 462 | k = 0 463 | while k < 1: 464 | k = 1 465 | ## rotate anti-clockwise 466 | new_v = [] 467 | for v in moved_vectors: 468 | new_v.append(transform_vector_degree(v, -.01)) 469 | new_dist = calculate_distance((new_v, matched_points_in_b)) 470 | if new_dist < moved_dist: 471 | moved_vectors = new_v 472 | moved_dist = new_dist 473 | else: 474 | break 475 | 476 | j += 1 477 | # break 478 | 479 | print(moved_vectors) 480 | print(distance,moved_dist) 481 | 482 | # temp_points_in_a = [x.getPoint() for x in matched_points_in_a] 483 | centa2 = find_centroid(matched_points_in_a) 484 | centb2 = find_centroid(moved_vectors) 485 | 486 | final_vector = [] 487 | fine_vector = [] 488 | supafine_vector = [] 489 | cent_original = find_centroid(problem.getInitialVectorA()) 490 | for v in problem.getInitialVectorA(): 491 | fine_vector.append(subtract_vectors(centa, v)) 492 | 493 | for v in fine_vector: 494 | supafine_vector.append(transform_vector(v, centa, centb2)) 495 | 496 | for v in supafine_vector: 497 | final_vector.append(add_vectors(v, centa)) 498 | # for v in problem.getInitialVectorA(): 499 | # final_vector.append(transform_vector(v, centa, centb)) 500 | 501 | print(calculate_distance((final_vector, problem.getInitialVectorB()))) 502 | print(final_vector) 503 | 504 | 505 | # # ## the rotation and alignment might be a bit off 506 | # # ## we'll brute force by moving it along the axis and then rotate 507 | # # ## until it's in the right place 508 | # distance = calculate_distance((rotated_vectors, sections[1])) 509 | # moved_vectors = rotated_vectors[:] 510 | # moved_dist = distance 511 | # j = 0 512 | # while j < 1000: 513 | # # while moved_dist > 100: 514 | # moving = True 515 | # while moving: 516 | # moving = False 517 | # while True: 518 | # ## move right 519 | # new_v = [] 520 | # for v in moved_vectors: 521 | # new_v.append(add_vectors(v, (.5,0))) 522 | # new_dist = calculate_distance((new_v, sections[1])) 523 | # if new_dist < moved_dist: 524 | # moved_vectors = new_v 525 | # moved_dist = new_dist 526 | # moving = True 527 | # else: 528 | # break 529 | # while True: 530 | # ## move up 531 | # new_v = [] 532 | # for v in moved_vectors: 533 | # new_v.append(add_vectors(v, (0,.5))) 534 | # new_dist = calculate_distance((new_v, sections[1])) 535 | # if new_dist < moved_dist: 536 | # moved_vectors = new_v 537 | # moved_dist = new_dist 538 | # moving = True 539 | # else: 540 | # break 541 | # while True: 542 | # ## move left 543 | # new_v = [] 544 | # for v in moved_vectors: 545 | # new_v.append(add_vectors(v, (-.5,0))) 546 | # new_dist = calculate_distance((new_v, sections[1])) 547 | # if new_dist < moved_dist: 548 | # moved_vectors = new_v 549 | # moved_dist = new_dist 550 | # moving = True 551 | # else: 552 | # break 553 | # while True: 554 | # ## move down 555 | # new_v = [] 556 | # for v in moved_vectors: 557 | # new_v.append(add_vectors(v, (0,-.5))) 558 | # new_dist = calculate_distance((new_v, sections[1])) 559 | # if new_dist < moved_dist: 560 | # moved_vectors = new_v 561 | # moved_dist = new_dist 562 | # moving = True 563 | # else: 564 | # break 565 | # 566 | # ## rotate 567 | # k = 0 568 | # while k < 1: 569 | # k = 1 570 | # ## rotate clockwise 571 | # new_v = [] 572 | # for v in moved_vectors: 573 | # new_v.append(transform_vector_degree(v, .01)) 574 | # new_dist = calculate_distance((new_v, sections[1])) 575 | # if new_dist < moved_dist: 576 | # moved_vectors = new_v 577 | # moved_dist = new_dist 578 | # else: 579 | # break 580 | # k = 0 581 | # while k < 1: 582 | # k = 1 583 | # ## rotate anti-clockwise 584 | # new_v = [] 585 | # for v in moved_vectors: 586 | # new_v.append(transform_vector_degree(v, -.01)) 587 | # new_dist = calculate_distance((new_v, sections[1])) 588 | # if new_dist < moved_dist: 589 | # moved_vectors = new_v 590 | # moved_dist = new_dist 591 | # else: 592 | # break 593 | # 594 | # j += 1 595 | # # break 596 | # 597 | # print(moved_vectors) 598 | # print(distance,moved_dist) 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | # print("matches", matches) 615 | # 616 | # a = (-3.9, 49.5) 617 | # b = (25.5, 42.7) 618 | # 619 | # ## 620 | # averages = [] 621 | # for i in range(0, 2): 622 | # 623 | # averagex = 0 624 | # averagey = 0 625 | # 626 | # for j in range(0, n[i]): 627 | # averagex += sections[i][j][0] 628 | # averagey += sections[i][j][1] 629 | # 630 | # averagex /= len(sections[i]) 631 | # averagey /= len(sections[i]) 632 | # 633 | # averages.append((averagex, averagey)) 634 | # # print(averagex, averagey) 635 | # 636 | # #move centroid to origin 637 | # origin = [] 638 | # for i in range(0, n[0]): 639 | # origin.append(subtract_vectors(averages[0], sections[0][i])) 640 | # 641 | # print("origin", origin) 642 | # 643 | # # #align the centroids 644 | # # R = rotation_matrix(a, b) 645 | # # rotated_vectors = [] 646 | # # for i in range(0, n[0]): 647 | # # rotated_vectors.append(transform_vector(sections[0][i], averages[0], averages[1])) 648 | # ## 649 | # 650 | # 651 | # # transform vectors 652 | # resulted_vectors = [] 653 | # for v in origin: 654 | # resulted_vectors.append(transform_vector(v, a, b)) 655 | # 656 | # #move centroid back 657 | # origin = [] 658 | # for i in range(0, n[0]): 659 | # origin.append(add_vectors(averages[0], resulted_vectors[i])) 660 | # 661 | # print(origin) 662 | # ############################################################################## --------------------------------------------------------------------------------