Algorithm Learning (v)

Source: Internet
Author: User
Tags x2 y2

1.Caesar Shift Cipher (password)

Description: Cryptography is one of the most interesting branches of programming. The algorithm is usually started in a simple way, named after the famous Roman Emperor Julius Caesar, who uses this method to convey his military secrets.

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

The following rules are:
1. Locate the letter in the alphabet (it should be encrypted);

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

3. Take the new letter from here;

4. If "move" encounters the end position of the algorithm, it can continue from its inception.

For example, if k=3, so that a corresponds to d,b corresponds to e,w corresponding to the z,z corresponding to C, as 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
Veni VIDI vici. yhql ylgl YLFL.
On the other hand, the encrypted information should be "moved back" to decode, or the 26-k move, which is the same.
even you Brutus.
Input data will contain two integers-the number of encrypted lines of text and the value of K .
It will then contain the encrypted text, consisting of Latin capital letters A through Z, each line being the terminating point. should be decoded in case of emergency. The answer should contain the decrypted message (in a single line, no line splitting is required).
input data:2 3yhql YLGL YLFL. HYHQ BRX EUXWXV.answer:VENI VIDI vici. Even you Brutus.

The code is as follows:

1Arys =['mxkktlokrjy 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 8K = 69Alphabet1 ='abcdefghijklmnopqrstuvwxyz' # Original AlphabetTenAlphabet2 = alphabet1[k:] +Alphabet1[:k] # The alphabet after the move One  A  -  forTEXT1inchArys: -TEXT2 = text1[:-1] theTEXT = Text2.split (' '# Divide the text into a single string, the word ' ZNK ', ' GTJ ' -Arrays = [] -      forWordinchTEXT: -List =[] # divide individual words into characters ' Z ', ' N ', ' K ' +          forOldinchWord: -             ifOldinchAlphabet2: +New =Alphabet1[alphabet2.index (old)] # find the corresponding letter A list.append (new) atNew_word ="'. Join (List) # merge into words - arrays.append (New_word) -Ans ='{}{}'. Format (' '. Join (Arrays),'.'# Format The string as required, with a comma at the end of '. ' -     Print(Ans, end=' ') -      - #Output result: Greenfields is 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 is wonders many told.
As easy as lying who WANTS to LIVE FOREVER in ANCIENT Persia there is A KING. A Night at the OPERA the DEAD BURY their OWN DEAD. A Day at the RACES GIVE YOUR ROOKS and not dilaram.

Alternatively, by using Ord (' A '), the ASCII value of the corresponding character can be found, and the new character is obtained by adding and minus the K value. Note, however, that uppercase letters have an ASCII value of 65 to 90, noting the conditions of the case.

2.Triangle Area (triangular)

Description: It is important to be able to calculate the area of a triangle, because many of the more complex tasks are usually easy to simplify (and we will use it later). One of the oldest known methods is the Heron formula, which takes the length of the triangular edge as input.

In this problem, you write a program that uses the x and y coordinates of the 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 6 values, followed by X1 Y1 X2 Y2 X3 Y3, which describes the three vertices of a triangle.
The answer should give a space-delimited triangle area (with an estimated accuracy of 1e-7).

For example:

Data:3 9 5 6 01 0 0 1 10000 100007886 5954 9953 2425 6250 2108Answer:17 9999.5 6861563

Helen Formula: Helen Formula It is a formula that uses the triangle's three-side side length to directly find the triangular area. The expression is: S=√p (p-a) (p-b) (p-c), it is characterized by a beautiful form, easy to remember.

Suppose in the plane, there is a triangle, the edge length is a, B, C, the area of the Triangle S can be calculated by the following formula: S=√p (P-A) (p-b) (P-C)

The p in the formula is half-perimeter: p= (a+b+c)/2, all you can do is calculate the area of the triangle by finding the three sides of the triangle by three points.

The code is as follows:

1 ImportMath #导入math模块, you need to use sqrt to open square2 3Arrays = [[1407, 1016, 6823, 9818, 5982, 9430],4[7789, 3696, 6561, 5583, 1463, 4274],5[3235, 9476, 4992, 822, 4024, 4507],6[5970, 7162, 352, 8567, 9296, 1582],7[5066, 2462, 8114, 7451, 4732, 2113]]8 9 Ten  forIinchRange (len (Arrays)): OneA = Math.sqrt ((arrays[i][0]-arrays[i][2]) * * 2 + (arrays[i][1]-arrays[i][3]) * * 2) # Computes the side length of the triangle Ab = Math.sqrt ((arrays[i][2]-arrays[i][4]) * * 2 + (Arrays[i][3]-arrays[i][5]) * * 2) -c = math.sqrt ((arrays[i][4]-arrays[i][0]) * * 2 + (Arrays[i][5]-arrays[i][1]) * * 2) -p = (A + B + c)/2 # half Perimeter theS = MATH.SQRT (P * (p-a) * (p-b) * (P-c) # Calculate Triangle area with Helen Formula -     Print(S, end=' ') - #output Result: 2650536.9999999963 5613689.000000002 951263.5000000321 13337704.999999976 301287.0000000146

Algorithm Learning (v)

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.