Python dictionary caesar: look up a numerical value, +1, then return its associated key
This is a really interesting caesar-cipher problem.
Here's my dictionary:
**{'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}**
Obviously, dictionaries are un-ordered. They're not organised alphabetically or numerically, only by pair.
I have to:
Look up each letter in a string("hi my name is caesar") in this dictionary
Return an encoded message. If the letter is 'a', this is value 1 in the dictionary. However, my code would be to print the letter 'b' which is value 2 in the dictionary.
I've seen examples of caesars using lists (which can be ordered by me), but not trying to navigate dictionaries in this way...
I will post my answer as soon as I have figured this out. At the moment I am just puzzled by it. Writing this down has helped clarify the steps that might be needed.
NB I think " " turns to "a" if the code works correctly.
expected output: "ijanzaobnajtadbftbt"
python string dictionary
|
show 2 more comments
This is a really interesting caesar-cipher problem.
Here's my dictionary:
**{'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}**
Obviously, dictionaries are un-ordered. They're not organised alphabetically or numerically, only by pair.
I have to:
Look up each letter in a string("hi my name is caesar") in this dictionary
Return an encoded message. If the letter is 'a', this is value 1 in the dictionary. However, my code would be to print the letter 'b' which is value 2 in the dictionary.
I've seen examples of caesars using lists (which can be ordered by me), but not trying to navigate dictionaries in this way...
I will post my answer as soon as I have figured this out. At the moment I am just puzzled by it. Writing this down has helped clarify the steps that might be needed.
NB I think " " turns to "a" if the code works correctly.
expected output: "ijanzaobnajtadbftbt"
python string dictionary
You should show your Minimal, Complete, and Verifiable example that attempts to use this dict.
– jdv
Nov 13 '18 at 16:18
4
So... are you planning to self answer this question or do you want us to spoil the solution? (edit: also, what's the expected output/result for"hi my name is caesar"
?)
– timgeb
Nov 13 '18 at 16:19
2
not sure if this is SO material...
– Capn Jack
Nov 13 '18 at 16:20
1
So if I understand correctly: I have lettera
and in the encoded version this should be a 2? What happens for example with y? Does it become a 26?
– user8408080
Nov 13 '18 at 16:20
stackoverflow.com/search?q=caeser+python
– jdv
Nov 13 '18 at 16:28
|
show 2 more comments
This is a really interesting caesar-cipher problem.
Here's my dictionary:
**{'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}**
Obviously, dictionaries are un-ordered. They're not organised alphabetically or numerically, only by pair.
I have to:
Look up each letter in a string("hi my name is caesar") in this dictionary
Return an encoded message. If the letter is 'a', this is value 1 in the dictionary. However, my code would be to print the letter 'b' which is value 2 in the dictionary.
I've seen examples of caesars using lists (which can be ordered by me), but not trying to navigate dictionaries in this way...
I will post my answer as soon as I have figured this out. At the moment I am just puzzled by it. Writing this down has helped clarify the steps that might be needed.
NB I think " " turns to "a" if the code works correctly.
expected output: "ijanzaobnajtadbftbt"
python string dictionary
This is a really interesting caesar-cipher problem.
Here's my dictionary:
**{'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}**
Obviously, dictionaries are un-ordered. They're not organised alphabetically or numerically, only by pair.
I have to:
Look up each letter in a string("hi my name is caesar") in this dictionary
Return an encoded message. If the letter is 'a', this is value 1 in the dictionary. However, my code would be to print the letter 'b' which is value 2 in the dictionary.
I've seen examples of caesars using lists (which can be ordered by me), but not trying to navigate dictionaries in this way...
I will post my answer as soon as I have figured this out. At the moment I am just puzzled by it. Writing this down has helped clarify the steps that might be needed.
NB I think " " turns to "a" if the code works correctly.
expected output: "ijanzaobnajtadbftbt"
python string dictionary
python string dictionary
edited Nov 13 '18 at 16:33
avwinter
asked Nov 13 '18 at 16:15
avwinteravwinter
435
435
You should show your Minimal, Complete, and Verifiable example that attempts to use this dict.
– jdv
Nov 13 '18 at 16:18
4
So... are you planning to self answer this question or do you want us to spoil the solution? (edit: also, what's the expected output/result for"hi my name is caesar"
?)
– timgeb
Nov 13 '18 at 16:19
2
not sure if this is SO material...
– Capn Jack
Nov 13 '18 at 16:20
1
So if I understand correctly: I have lettera
and in the encoded version this should be a 2? What happens for example with y? Does it become a 26?
– user8408080
Nov 13 '18 at 16:20
stackoverflow.com/search?q=caeser+python
– jdv
Nov 13 '18 at 16:28
|
show 2 more comments
You should show your Minimal, Complete, and Verifiable example that attempts to use this dict.
– jdv
Nov 13 '18 at 16:18
4
So... are you planning to self answer this question or do you want us to spoil the solution? (edit: also, what's the expected output/result for"hi my name is caesar"
?)
– timgeb
Nov 13 '18 at 16:19
2
not sure if this is SO material...
– Capn Jack
Nov 13 '18 at 16:20
1
So if I understand correctly: I have lettera
and in the encoded version this should be a 2? What happens for example with y? Does it become a 26?
– user8408080
Nov 13 '18 at 16:20
stackoverflow.com/search?q=caeser+python
– jdv
Nov 13 '18 at 16:28
You should show your Minimal, Complete, and Verifiable example that attempts to use this dict.
– jdv
Nov 13 '18 at 16:18
You should show your Minimal, Complete, and Verifiable example that attempts to use this dict.
– jdv
Nov 13 '18 at 16:18
4
4
So... are you planning to self answer this question or do you want us to spoil the solution? (edit: also, what's the expected output/result for
"hi my name is caesar"
?)– timgeb
Nov 13 '18 at 16:19
So... are you planning to self answer this question or do you want us to spoil the solution? (edit: also, what's the expected output/result for
"hi my name is caesar"
?)– timgeb
Nov 13 '18 at 16:19
2
2
not sure if this is SO material...
– Capn Jack
Nov 13 '18 at 16:20
not sure if this is SO material...
– Capn Jack
Nov 13 '18 at 16:20
1
1
So if I understand correctly: I have letter
a
and in the encoded version this should be a 2? What happens for example with y? Does it become a 26?– user8408080
Nov 13 '18 at 16:20
So if I understand correctly: I have letter
a
and in the encoded version this should be a 2? What happens for example with y? Does it become a 26?– user8408080
Nov 13 '18 at 16:20
stackoverflow.com/search?q=caeser+python
– jdv
Nov 13 '18 at 16:28
stackoverflow.com/search?q=caeser+python
– jdv
Nov 13 '18 at 16:28
|
show 2 more comments
4 Answers
4
active
oldest
votes
You can create a reversed dictionary and use that to build your encrypted string. The reversed dictionary will be helpful to look up the character corresponding to the "next" number for a given character:
d = {'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}
reverse_d = {v: k for k, v in d.items()}
sentence = 'hi my name is caesar'
encrypted = ''.join([reverse_d.get(d[c] + 1, reverse_d[0]) for c in sentence])
print(encrypted)
# ijanzaobnfajtadbftbs
Note that I use the default key 0
if a number isn't found in reverse_d
.
add a comment |
If the dictionary always just denotes the number of the character in the alphabet, you don't even need the dictionary and this is a one-liner:
sentence = 'hi my name is caesar'
encoded = ''.join([chr(ord(c)+1) if ord(c)>96 and ord(c)<122 else "a" for c in sentence])
This gives
In [14]: encoded
Out[14]: 'ijanzaobnfajtadbftbs'
But for a general solution, slider's approach is the most elegant, I would say.
add a comment |
okay here is my take
d = {'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}
sentence = 'hi my name is caesar'
for char in ''.join(sentence.split()):
print(d[chr(ord(char) + 1)], end=' ')
Output
9 10 14 26 15 2 14 6 10 20 4 2 6 20 2 19
You are not usingd
anywhere?
– timgeb
Nov 13 '18 at 16:37
@timgeb updated..thanks
– iamklaus
Nov 14 '18 at 4:40
add a comment |
message = "hi my name is caesar"
alphabet = ' abcdefghijklmnopqrstuvwxyz'
positions = {' ': 0,'a': 1,'b': 2,'c': 3,'d': 4,'e': 5,'f': 6,'g': 7,'h': 8,'i': 9,'j': 10,'k': 11,'l': 12,'m': 13,'n': 14,'o': 15,'p': 16,'q': 17,'r': 18,'s': 19,'t': 20,'u': 21,'v': 22,'w': 23,'x': 24,'y': 25,'z': 26}
# STEP ONE is to make an empty list to add your characters to. (Lists work well with dictionaries)
encryptlist =
# STEP TWO locate the value of the 'r' in caesar.
for chars in message:
Num = positions[chars]
Num returns: 18
#STEP THREE encrypt by one step (+1). You can keep the result within 0-26 using result % 27
encoded_Num = (Num + 1) % 27
#STEP 4 encrypt as a list
encryptlist.append(alphabet[encoded_Num])
encryptlist now returns
['i', 'j', 'a', 'n', 'z', 'a', 'o', 'b', 'n', 'f', 'a', 'j', 't', 'a', 'd', 'b', 'f', 't', 'b', 's']
# convert the list to a string
encoded_message = "".join(encryptlist)
encoded_message
returns: 'ijanzaobnfajtadbftbs' which is the expected output
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285175%2fpython-dictionary-caesar-look-up-a-numerical-value-1-then-return-its-associa%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can create a reversed dictionary and use that to build your encrypted string. The reversed dictionary will be helpful to look up the character corresponding to the "next" number for a given character:
d = {'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}
reverse_d = {v: k for k, v in d.items()}
sentence = 'hi my name is caesar'
encrypted = ''.join([reverse_d.get(d[c] + 1, reverse_d[0]) for c in sentence])
print(encrypted)
# ijanzaobnfajtadbftbs
Note that I use the default key 0
if a number isn't found in reverse_d
.
add a comment |
You can create a reversed dictionary and use that to build your encrypted string. The reversed dictionary will be helpful to look up the character corresponding to the "next" number for a given character:
d = {'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}
reverse_d = {v: k for k, v in d.items()}
sentence = 'hi my name is caesar'
encrypted = ''.join([reverse_d.get(d[c] + 1, reverse_d[0]) for c in sentence])
print(encrypted)
# ijanzaobnfajtadbftbs
Note that I use the default key 0
if a number isn't found in reverse_d
.
add a comment |
You can create a reversed dictionary and use that to build your encrypted string. The reversed dictionary will be helpful to look up the character corresponding to the "next" number for a given character:
d = {'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}
reverse_d = {v: k for k, v in d.items()}
sentence = 'hi my name is caesar'
encrypted = ''.join([reverse_d.get(d[c] + 1, reverse_d[0]) for c in sentence])
print(encrypted)
# ijanzaobnfajtadbftbs
Note that I use the default key 0
if a number isn't found in reverse_d
.
You can create a reversed dictionary and use that to build your encrypted string. The reversed dictionary will be helpful to look up the character corresponding to the "next" number for a given character:
d = {'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}
reverse_d = {v: k for k, v in d.items()}
sentence = 'hi my name is caesar'
encrypted = ''.join([reverse_d.get(d[c] + 1, reverse_d[0]) for c in sentence])
print(encrypted)
# ijanzaobnfajtadbftbs
Note that I use the default key 0
if a number isn't found in reverse_d
.
edited Nov 13 '18 at 16:50
answered Nov 13 '18 at 16:44
sliderslider
8,26811130
8,26811130
add a comment |
add a comment |
If the dictionary always just denotes the number of the character in the alphabet, you don't even need the dictionary and this is a one-liner:
sentence = 'hi my name is caesar'
encoded = ''.join([chr(ord(c)+1) if ord(c)>96 and ord(c)<122 else "a" for c in sentence])
This gives
In [14]: encoded
Out[14]: 'ijanzaobnfajtadbftbs'
But for a general solution, slider's approach is the most elegant, I would say.
add a comment |
If the dictionary always just denotes the number of the character in the alphabet, you don't even need the dictionary and this is a one-liner:
sentence = 'hi my name is caesar'
encoded = ''.join([chr(ord(c)+1) if ord(c)>96 and ord(c)<122 else "a" for c in sentence])
This gives
In [14]: encoded
Out[14]: 'ijanzaobnfajtadbftbs'
But for a general solution, slider's approach is the most elegant, I would say.
add a comment |
If the dictionary always just denotes the number of the character in the alphabet, you don't even need the dictionary and this is a one-liner:
sentence = 'hi my name is caesar'
encoded = ''.join([chr(ord(c)+1) if ord(c)>96 and ord(c)<122 else "a" for c in sentence])
This gives
In [14]: encoded
Out[14]: 'ijanzaobnfajtadbftbs'
But for a general solution, slider's approach is the most elegant, I would say.
If the dictionary always just denotes the number of the character in the alphabet, you don't even need the dictionary and this is a one-liner:
sentence = 'hi my name is caesar'
encoded = ''.join([chr(ord(c)+1) if ord(c)>96 and ord(c)<122 else "a" for c in sentence])
This gives
In [14]: encoded
Out[14]: 'ijanzaobnfajtadbftbs'
But for a general solution, slider's approach is the most elegant, I would say.
answered Nov 13 '18 at 17:43
user8408080user8408080
1,264139
1,264139
add a comment |
add a comment |
okay here is my take
d = {'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}
sentence = 'hi my name is caesar'
for char in ''.join(sentence.split()):
print(d[chr(ord(char) + 1)], end=' ')
Output
9 10 14 26 15 2 14 6 10 20 4 2 6 20 2 19
You are not usingd
anywhere?
– timgeb
Nov 13 '18 at 16:37
@timgeb updated..thanks
– iamklaus
Nov 14 '18 at 4:40
add a comment |
okay here is my take
d = {'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}
sentence = 'hi my name is caesar'
for char in ''.join(sentence.split()):
print(d[chr(ord(char) + 1)], end=' ')
Output
9 10 14 26 15 2 14 6 10 20 4 2 6 20 2 19
You are not usingd
anywhere?
– timgeb
Nov 13 '18 at 16:37
@timgeb updated..thanks
– iamklaus
Nov 14 '18 at 4:40
add a comment |
okay here is my take
d = {'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}
sentence = 'hi my name is caesar'
for char in ''.join(sentence.split()):
print(d[chr(ord(char) + 1)], end=' ')
Output
9 10 14 26 15 2 14 6 10 20 4 2 6 20 2 19
okay here is my take
d = {'v': 22, 'g': 7, 'w': 23, 'h': 8, 'a': 1, 'm': 13, 'c': 3, 'o': 15, 'd': 4, 's': 19, 'r': 18, 'u': 21, 'j': 10, 't': 20, 'f': 6, 'k': 11, 'y': 25, 'z': 26, 'l': 12, ' ': 0, 'b': 2, 'e': 5, 'q': 17, 'n': 14, 'i': 9, 'p': 16, 'x': 24}
sentence = 'hi my name is caesar'
for char in ''.join(sentence.split()):
print(d[chr(ord(char) + 1)], end=' ')
Output
9 10 14 26 15 2 14 6 10 20 4 2 6 20 2 19
edited Nov 14 '18 at 4:40
answered Nov 13 '18 at 16:25
iamklausiamklaus
978149
978149
You are not usingd
anywhere?
– timgeb
Nov 13 '18 at 16:37
@timgeb updated..thanks
– iamklaus
Nov 14 '18 at 4:40
add a comment |
You are not usingd
anywhere?
– timgeb
Nov 13 '18 at 16:37
@timgeb updated..thanks
– iamklaus
Nov 14 '18 at 4:40
You are not using
d
anywhere?– timgeb
Nov 13 '18 at 16:37
You are not using
d
anywhere?– timgeb
Nov 13 '18 at 16:37
@timgeb updated..thanks
– iamklaus
Nov 14 '18 at 4:40
@timgeb updated..thanks
– iamklaus
Nov 14 '18 at 4:40
add a comment |
message = "hi my name is caesar"
alphabet = ' abcdefghijklmnopqrstuvwxyz'
positions = {' ': 0,'a': 1,'b': 2,'c': 3,'d': 4,'e': 5,'f': 6,'g': 7,'h': 8,'i': 9,'j': 10,'k': 11,'l': 12,'m': 13,'n': 14,'o': 15,'p': 16,'q': 17,'r': 18,'s': 19,'t': 20,'u': 21,'v': 22,'w': 23,'x': 24,'y': 25,'z': 26}
# STEP ONE is to make an empty list to add your characters to. (Lists work well with dictionaries)
encryptlist =
# STEP TWO locate the value of the 'r' in caesar.
for chars in message:
Num = positions[chars]
Num returns: 18
#STEP THREE encrypt by one step (+1). You can keep the result within 0-26 using result % 27
encoded_Num = (Num + 1) % 27
#STEP 4 encrypt as a list
encryptlist.append(alphabet[encoded_Num])
encryptlist now returns
['i', 'j', 'a', 'n', 'z', 'a', 'o', 'b', 'n', 'f', 'a', 'j', 't', 'a', 'd', 'b', 'f', 't', 'b', 's']
# convert the list to a string
encoded_message = "".join(encryptlist)
encoded_message
returns: 'ijanzaobnfajtadbftbs' which is the expected output
add a comment |
message = "hi my name is caesar"
alphabet = ' abcdefghijklmnopqrstuvwxyz'
positions = {' ': 0,'a': 1,'b': 2,'c': 3,'d': 4,'e': 5,'f': 6,'g': 7,'h': 8,'i': 9,'j': 10,'k': 11,'l': 12,'m': 13,'n': 14,'o': 15,'p': 16,'q': 17,'r': 18,'s': 19,'t': 20,'u': 21,'v': 22,'w': 23,'x': 24,'y': 25,'z': 26}
# STEP ONE is to make an empty list to add your characters to. (Lists work well with dictionaries)
encryptlist =
# STEP TWO locate the value of the 'r' in caesar.
for chars in message:
Num = positions[chars]
Num returns: 18
#STEP THREE encrypt by one step (+1). You can keep the result within 0-26 using result % 27
encoded_Num = (Num + 1) % 27
#STEP 4 encrypt as a list
encryptlist.append(alphabet[encoded_Num])
encryptlist now returns
['i', 'j', 'a', 'n', 'z', 'a', 'o', 'b', 'n', 'f', 'a', 'j', 't', 'a', 'd', 'b', 'f', 't', 'b', 's']
# convert the list to a string
encoded_message = "".join(encryptlist)
encoded_message
returns: 'ijanzaobnfajtadbftbs' which is the expected output
add a comment |
message = "hi my name is caesar"
alphabet = ' abcdefghijklmnopqrstuvwxyz'
positions = {' ': 0,'a': 1,'b': 2,'c': 3,'d': 4,'e': 5,'f': 6,'g': 7,'h': 8,'i': 9,'j': 10,'k': 11,'l': 12,'m': 13,'n': 14,'o': 15,'p': 16,'q': 17,'r': 18,'s': 19,'t': 20,'u': 21,'v': 22,'w': 23,'x': 24,'y': 25,'z': 26}
# STEP ONE is to make an empty list to add your characters to. (Lists work well with dictionaries)
encryptlist =
# STEP TWO locate the value of the 'r' in caesar.
for chars in message:
Num = positions[chars]
Num returns: 18
#STEP THREE encrypt by one step (+1). You can keep the result within 0-26 using result % 27
encoded_Num = (Num + 1) % 27
#STEP 4 encrypt as a list
encryptlist.append(alphabet[encoded_Num])
encryptlist now returns
['i', 'j', 'a', 'n', 'z', 'a', 'o', 'b', 'n', 'f', 'a', 'j', 't', 'a', 'd', 'b', 'f', 't', 'b', 's']
# convert the list to a string
encoded_message = "".join(encryptlist)
encoded_message
returns: 'ijanzaobnfajtadbftbs' which is the expected output
message = "hi my name is caesar"
alphabet = ' abcdefghijklmnopqrstuvwxyz'
positions = {' ': 0,'a': 1,'b': 2,'c': 3,'d': 4,'e': 5,'f': 6,'g': 7,'h': 8,'i': 9,'j': 10,'k': 11,'l': 12,'m': 13,'n': 14,'o': 15,'p': 16,'q': 17,'r': 18,'s': 19,'t': 20,'u': 21,'v': 22,'w': 23,'x': 24,'y': 25,'z': 26}
# STEP ONE is to make an empty list to add your characters to. (Lists work well with dictionaries)
encryptlist =
# STEP TWO locate the value of the 'r' in caesar.
for chars in message:
Num = positions[chars]
Num returns: 18
#STEP THREE encrypt by one step (+1). You can keep the result within 0-26 using result % 27
encoded_Num = (Num + 1) % 27
#STEP 4 encrypt as a list
encryptlist.append(alphabet[encoded_Num])
encryptlist now returns
['i', 'j', 'a', 'n', 'z', 'a', 'o', 'b', 'n', 'f', 'a', 'j', 't', 'a', 'd', 'b', 'f', 't', 'b', 's']
# convert the list to a string
encoded_message = "".join(encryptlist)
encoded_message
returns: 'ijanzaobnfajtadbftbs' which is the expected output
answered Nov 28 '18 at 17:02
avwinteravwinter
435
435
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285175%2fpython-dictionary-caesar-look-up-a-numerical-value-1-then-return-its-associa%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You should show your Minimal, Complete, and Verifiable example that attempts to use this dict.
– jdv
Nov 13 '18 at 16:18
4
So... are you planning to self answer this question or do you want us to spoil the solution? (edit: also, what's the expected output/result for
"hi my name is caesar"
?)– timgeb
Nov 13 '18 at 16:19
2
not sure if this is SO material...
– Capn Jack
Nov 13 '18 at 16:20
1
So if I understand correctly: I have letter
a
and in the encoded version this should be a 2? What happens for example with y? Does it become a 26?– user8408080
Nov 13 '18 at 16:20
stackoverflow.com/search?q=caeser+python
– jdv
Nov 13 '18 at 16:28