mhw (2) [Avatar] Offline
#1
Here is another solution using strings.Repeat:

package main

import "fmt"
import "strings"

func main() {
	cipherText := "CSOITEUIWUIZNSROCNKFD"
	keyword := "GOLANG"
	s := len(cipherText)
	k := len(keyword)
	key := strings.Repeat(keyword, s/k) + keyword[:s%k] // key = "GOLANGGOLANGGOLANGGOL"
	message := ""

	for i, _ := range cipherText {
		if cipherText[i] >= key[i] {
			message += string('A' + (cipherText[i] - key[i]))
		} else {
			message += string('Z' + 1 + (cipherText[i] - key[i]))
		}
	}

	fmt.Println(message)
}


Link to the solution in Go's Playground: https://play.golang.org/p/Em-lUGjRjL
Nathan Youngman (37) [Avatar] Offline
#2
mhw wrote:Here is another solution using strings.Repeat:

package main

import "fmt"
import "strings"

func main() {
	cipherText := "CSOITEUIWUIZNSROCNKFD"
	keyword := "GOLANG"
	s := len(cipherText)
	k := len(keyword)
	key := strings.Repeat(keyword, s/k) + keyword[:s%k] // key = "GOLANGGOLANGGOLANGGOL"
	message := ""

	for i, _ := range cipherText {
		if cipherText[i] >= key[i] {
			message += string('A' + (cipherText[i] - key[i]))
		} else {
			message += string('Z' + 1 + (cipherText[i] - key[i]))
		}
	}

	fmt.Println(message)
}


Link to the solution in Go's Playground: https://play.golang.org/p/Em-lUGjRjL


Interesting approach and nice work using the slice syntax as well.

How would you encode a string though? I'm not entirely sure about the code:
cipherText[i] >= key[i]


It looks like this code is intended to both cipher and decipher text, but if so, there is a bug. Try deciphering what's there, then run that text through the program, and then run that result through again.
mhw (2) [Avatar] Offline
#3
For ciphering and deciphering we need two different functions. Here is the modified program with two functions cipher and decipher:


package main

import "fmt"
import "strings"

func decipher(cipherT string, keyW string) string {
	message := ""
	for i, _ := range cipherT {
		if cipherT[i] >= keyW[i] {
			message += string('A' + (cipherT[i] - keyW[i]))
		} else {
			message += string('Z' + 1 + (cipherT[i] - keyW[i]))
		}
	}
	return message
}

func cipher(plainT string, keyW string) string {
	cipherT := ""
	for i, _ := range plainT {
		if plainT[i] + (keyW[i]-'A') <= 'Z' {
			cipherT += string(plainT[i] + (keyW[i]-'A'))
		} else {
			cipherT += string(plainT[i] + (keyW[i]-'Z'-1))
		}
	}
	return cipherT
}

func main() {
	cipherText := "CSOITEUIWUIZNSROCNKFD"
	keyword := "GOLANG"
	s := len(cipherText)
	k := len(keyword)
	key := strings.Repeat(keyword, s/k) + keyword[:s%k] // key = "GOLANGGOLANGGOLANGGOL" ""
	
	plainText := decipher(cipherText , key)
	newCiphertext := cipher(plainText, key)

	fmt.Println("ciphertext: ", cipherText )
	fmt.Println("original message: ", plainText)
	fmt.Println("ciphering of original message: ", newCiphertext)
	
}



Concerning the condition if cipherText[i] >= key[i]:

In the following I write c[i] for ciphertext[i], m[i] for message[i] and k[i] for key[i].

From the definition of cipher we have either
(1) c[i] = m[i] + k[i] - 'A'
or
(2) c[i] = m[i] + k[i] - ('Z'+1).

Further 'A' <= m[i] <= 'Z'.

We have

(1) if and only if c[i] - k[i] = m[i] - 'A'

(2) if and only if c[i] - k[i] = m[i] - ('Z'+1)

As m[i] >= 'A' we get: (1) if and only if c[i] >= k[i]

As m[i] <= 'Z' we get: (2) if and only if c[i] < k[i].

Therefore from c[i] >= k[i] we get (1) and therefore we further get m[i] = c[i] + 'A' - k[i].

465469 (6) [Avatar] Offline
#4
blood pressure machine
This is when high blood pressure requires emergency medical attention..Symptoms of high blood pressure monitor ,
Blood pressure checker,
Blood pressure reading
If your blood pressure is higher than 180/110 mm Hg and you are NOT experiencing symptoms such as chest pain, shortness of breath, back pain, numbness/weakness, changes in vision or difficulty speaking,
High blood pressure symptoms
wait about five minutes and take it again. If the reading is still at or above that level, you should CALL 9-1-1 and get help immediately
Low blood pressure readings and
Blood pressure range
thanku