Algorithm Learning (6): algorithm Learning
1. Rock Paper Scissors (Scissors and stone cloth)
Note: There is a particularly important game in Computer Science. Because it is very simple, it can be used to create very tricky artificial intelligence algorithms to combat humans (or each other) and predict competitor behavior.
This old game was played between two participants who simultaneously used their gestures-stone, paper, or scissors. If both of them make the same gesture, the round is regarded as a draw. Otherwise, the following rules apply:
Rock beats Scissors (by blunting them)Scissors beat Paper (by cutting it)Paper beats Rock (by covering it)
The competition is usually played on the stairs. Players who have won the round take a step forward. The person who first reaches the end of the stairs is the winner.
You will get several game records. You want to tell them who wins.
Input data will contain the number of matches played in the first line. Then the matching description is written in separate rows.
Each row has several pairs of letters, such as PR (throwing paper for the first time, throwing stones for the second time), or SS (all scissors), separated from spaces.
Each matching Answer of Answer should specify whether the first player wins (value 1) or second (value 2) without a draw.
Example:
input data:3SS PRPR RS PS PP SPPS RR PS RPanswer:1 1 2
The Code is as follows:
1 condition = [['ss', 'rs ', 'pp', 'pp ', 'SP', 'ps ', 'rp', 'pp ', 'rr ', 'pp', 'sr', 'pp ', 'ps', 'ps '], 2 ['SP', 'rp ', 'ss ', 'rp ', 'pp', 'rs ', 'rr', 'rp '], 3 ['ps', 'pr', 'pp', 'rs ', 'ss', 'sr', 'sr', 'SP ', 'rp', 'rr ', 'pp', 'pr', 'sr'], 4 ['sr', 'SP ', 'ss', 'ss', 'rr', 'rr ', 'sr', 'sr', 'ss ', 'rp '], 5 ['ss', 'rr', 'ps ', 'pr', 'rs', 'rp ', 'pp ', 'rp '] 6 7 for I in range (len (condition): 8 win1 = 0 9 win2 = 010 for n in range (len (condition [I]): 11 if condition [I] [n] [0] = condition [I] [n] [1]: 12 continue13 elif condition [I] [n] [0] is's and condition [I] [n] [1] is 'P ': 14 win1 + = 115 elif condition [I] [n] [0] is 'R' and condition [I] [n] [1] is's ': 16 win1 ++ = 117 elif condition [I] [n] [0] is 'P' and condition [I] [n] [1] is 'r ': 18 win1 + = 119 else: 20 win2 + = 121 if win1> win2: 22 print ('1', end = '') 23 else: 24 print ('2 ', end = '') 25 26 output: 2 2 2 2 2
Your solution is not the optimal solution. You can directly define draw = ['ss', 'pp ', 'rr] win1 = ['SP', 'rs ', 'pr'] win2 = ['ps, 'sr', RP '] list of comparison characters.
2. Prime Numbers Generation (Prime number)
Note: In this task, we need to implement the prime number generator. A prime number is a positive integer and has no other factors except 1 and itself. You can read more in Wikipedia articles. The most popular algorithm isSieve of EratostheneAndTrial division.
Therefore, let's create a prime number array (or list) in ascending order, that is
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ...]
Prime numbers are not divisible by 2, 3, 5, and 7.
Then, the prime number corresponding to the given index in the input data is output.
Input data will contain the prime number printed in the first line.
The next line will contain the index of the prime number array used to print the value. They range from 1 to 0.2 million.
Answer should contain prime numbers corresponding to the specified position in the array.
Note: For this task, we start to index an array from 1 instead of 0 (this may help you check your program and have a list of prime numbers that can be found online ).
input data:47 1 199999 4answer:17 2 2750131 7
The Code is as follows:
1 nums = [182894,136 660, 173358,132 410, 199128,151 979, 102865,180 622, 150340,163 151] 2 3 def eladuosai (n): # iradose screening method 4 l = list (range (1, n + 1) 5 l [0] = 0 6 for I in range (2, n + 1): 7 if l [I-1]! = 0: 8 for j in range (I * 2, n + 1, I): 9 l [J-1] = 010 result = [x for x in l if x! = 0] 11 return result12 13 s = eladuosai (3000000) 14 for n in nums: 15 print (s [n-1], end = '') 16 17 output: 2497423 1823051 2357119 1761883 2737129 2043817 1340153 2463763 2020181 2207039
When the number of prime index locations is large, the efficiency of using the errado filter method is higher.