Algorithm Learning (5): algorithm Learning

Source: Internet
Author: User
Tags x2 y2

Algorithm Learning (5): algorithm Learning
1. Caesar Shift Cipher (password)

Note: cryptography is one of the most interesting branches in programming. The algorithm used to study it generally started with a simple method named after the famous Roman Emperor Caesar. He used this method to convey his military secrets.

We will practice decrypting encrypted messages in this issue. The idea of this algorithm is very simple. Each letter in the original text is replaced by another letter.

The following rules are as follows:
1. Find the letters in the alphabet (which should be encrypted );

2. Move the K position further (in the alphabet );

3. Get a new letter from here;

4. If "mobile" encounters an algorithm ending position, it can continue from its beginning.

For example, if K = 3, A corresponds to D, B Corresponds to E, W corresponds to Z, Z corresponds to C, as shown in the following table:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z| | | | | | | | | | | | | | | | | | | | | | | | | |V V V V V V V V V V V V V V V V V V V V V V V V V V D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
When the information isVeni vidi vici.Is encodedYHQL YLGL YLFL.
On the other hand, the encrypted information should be "Moved Back" to decode, or 26-K to move, which is the same.
Therefore, if we have the encoded message hyhq brx euxwxv, we can apply the conversion of 26-K = 26-3 = 23 to find the original text, that isEVEN YOU BRUTUS.
The Input data contains two integers: the number of encrypted text rows and the number of K values.
Next, the string contains the encrypted text, which is A to Z, and each line is terminated. Decoded in an emergency. The answer should contain the decrypted message (in a single row, there is no need to split the line ).
input data:2 3YHQL YLGL YLFL.HYHQ BRX EUXWXV.answer:VENI VIDI VICI. EVEN YOU BRUTUS.

The Code is as follows:

1 Arys = ['mxktlokrjy gxk mutk tuc znk ykixkz ul nkgznkx grk gtj luxmobk ay uax jkhzy. ', 2' znk utik gtj lazaxk qotm gtj yu eua zuu hxazay. ', 3' igrrkj oz znk xoyotm yat rubkyz znua sk vkzkx gxk cutjkxy sgte zurj. ', 4' gy kgye gy reotm cnu cgtzy zu robk luxkbkx ot gtioktz vkxyog znkxk cgy g qotm. ', 5' g tomnz gz znk uvkxg znk jkgj haxe znkox uct jkgj. ', 6' g jge gz znk xgiky mobk euax xuuqy haz tuz jorgxgs. '] 7 8 K = 6 9 alphabet1 = 'abcdefghijklmnopqrstuvwxy' # original alphabet 10 alphabet2 = alphabet1 [K:] + alphabet1 [: k] # The moved alphabet 11 12 13 for TEXT1 in Arys: 14 TEXT2 = TEXT1 [:-1] 15 TEXT = TEXT2.split ('') # divide the TEXT into a single string, 'znk', 'gtj' 16 Arrays = [] 17 for word in TEXT: 18 List = [] # divide a word into 'Z' characters ', 'N', 'K' 19 for old in word: 20 if old in alphabet2: 21 new = alphabet1 [alphabet2.index (old)] # Find the corresponding letter 22 List. append (new) 23 new_word = ''. join (List) # merge into 24 Arrays. append (new_word) 25 Ans = '{}{}'. format (''. join (Arrays ),'. ') # format the string as required with a comma at the end '. '26 print (Ans, end = '') 27 28 # output: greenfields are gone now the secret of heather ale and forgive us our debts. the once and future king and so you too brutus. called it the rising sun lovest thou me peter are wonders too TOLD.
As easy as lying who wants to live forever in your ent persia there was a king. a night at the opera the dead bury their own dead. a day at the races give your rooks but not dilaram.

Alternatively, you can use ord ('A') to find the ASCII value of the corresponding character, and add or subtract the K value to obtain A new character. Note that uppercase and lowercase letters contain ASCII values from 65 to 90.

 

2. Triangle Area (Triangle Area)

Note: It is very important to be able to calculate the area of a triangle, because many more complex tasks are often simplified to this (we will also use it later ). One of the oldest known methods is the Heron formula, which uses the length of the triangle edge as the input.

In this case, you need to write a program that uses the X and Y coordinates of triangle vertices. So you can use this formula or find another one.

Input data will contain the number of triangles to be processed.
The next line will contain six values, which are X1 Y1 X2 Y2 X3 Y3 in sequence and describe the three vertices of a triangle.
The answer should be a triangle area separated by space (with an expected accuracy of 1e-7 ).

For example:

data:31 3 9 5 6 01 0 0 1 10000 100007886 5954 9953 2425 6250 2108answer:17 9999.5 6861563

Helen formula: the formula used by Helen to calculate the area of a triangle using the side length of the three sides of the triangle. The expression is S = √ p (p-a) (p-B) (p-c). It features a beautiful form and is easy to remember.

Assume that there is a triangle in the plane, and the side length is a, B, and c respectively. The area S of the triangle can be obtained by the following formula: S = √ p (p-) (p-B) (p-c)

In the formula, p is the semi-perimeter: p = (a + B + c)/2, and all three sides of the triangle are obtained through three points, the area of the triangle can be calculated.

The Code is as follows:

1 import math # import the math module and use sqrt to square 2 3 Arrays = [[1407,101 6, 6823,981 8, 5982,943 0], 4 [7789,369 6, 6561,558 3, 1463,427 4], 5 [3235,947 6, 4992,822,402 4, 4507], 6 [5970,716 2, 352,856 7, 9296,158 2], 7 [5066,246 2, 8114,745 1, 4732,211 3] 8 9 10 for I in range (len (Arrays): 11 a = math. sqrt (Arrays [I] [0]-Arrays [I] [2]) ** 2 + (Arrays [I] [1]-Arrays [I] [3]) ** 2) # Calculate the side length of a triangle 12 B = math. sqrt (Arrays [I] [2]-Arrays [I] [4]) ** 2 + (Arrays [I] [3]-Arrays [I] [5]) ** 2) 13 c = math. sqrt (Arrays [I] [4]-Arrays [I] [0]) ** 2 + (Arrays [I] [5]-Arrays [I] [1]) ** 2) 14 p = (a + B + c)/2 # Calculate the half perimeter 15 S = math. sqrt (p * (p-a) * (p-B) * (p-c) # use Helen's formula to calculate the Triangle Area 16 print (S, end = '') 17 # output result: 2650536.9999999963 5613689.000000002 951263.5000000321 13337704.999999976 301287.0000000146

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.