<blockquote>
<p><strong>Bibliography:</strong><br/>
Kid Snippets: "Math Class" (Imagined by Kids) - YouTube <a href="http://youtu.be/KdxEAt91D7k" rel="nofollow">http://youtu.be/KdxEAt91D7k</a><br/>
Mary Had A Little Lamb Nursery Rhyme With Lyrics - YouTube <a href="http://youtu.be/CkRdvGmcCBE" rel="nofollow">http://youtu.be/CkRdvGmcCBE</a><br/>
Mary Had a Little Lamb - Wikipedia, the free encyclopedia <a href="http://goo.gl/FNEuyd" rel="nofollow">http://goo.gl/FNEuyd</a></p>
</blockquote>
<h2>Python源代码:</h2>
<blockquote>
<p>Note: <em>working for negative shift numbers also</em><br/>
Note: <em>if reverse shift then we do encode - decode message</em><br/>
Note: <em>preserving spaces also</em></p>
</blockquote>
<pre><code>small_chars = [chr(item) for item in range(ord('a'), ord('z')+1)]
upper_chars = [item.upper() for item in small_chars]
def encode_chr(chr_item, is_upper_case):
'''
Cipher each chr_item.
'''
# setting orig and end order.
if is_upper_case:
orig_ord = ord('A')
end_ord = ord('Z')
else:
orig_ord = ord('a')
end_ord = ord('z')
# calculating shift
temp_ord = ord(chr_item)+shift
# calculating offset order with modulo.
# char is after end_ord, calculating offset
num_of_chars = 26
offset_ord = (temp_ord - end_ord - 1)%num_of_chars
return chr(orig_ord + offset_ord)
# enable while loop to repeat until status not 'y'
status = 'y'
while status == 'y':
# enter word to cipher.
word = raw_input("Word: ")
# enter char shift
shift = input("Shift: ")
print
# create cipher list variable
cipher = list()
# loop trough each char in word
for chr_item in word:
# encode just letters.
# replace non-alfa with underscore: "_"
if chr_item in upper_chars or chr_item in small_chars:
# set is_uppser_case to True for upper case chars.
is_upper_case = (chr_item in upper_chars) and True
# cipher char.
temp_chr = encode_chr(chr_item, is_upper_case)
# append ciphered char to list
cipher.append(temp_chr)
elif chr_item is ' ':
cipher.append(chr_item)
else:
cipher.append('_')
# print word
print word
# print ciphered word
print ''.join(cipher)
# repeat again for another word?
status = raw_input("Repeat? [y|n]: ")
print
</code></pre>
<h2>测试用例:</h2>
^{pr2}$
<h2>输出:</h2>
<blockquote>
<h3>Note: <em>if reverse shift then we do encode - decode message</em></h3>
</blockquote>
<pre><code>>>>
Word: "Mary Had a Little Lamb"
Shift: 1
"Mary Had a Little Lamb"
_Nbsz Ibe b Mjuumf Mbnc_
Repeat? [y|n]: y
Word: _Nbsz Ibe b Mjuumf Mbnc_
Shift: -1
_Nbsz Ibe b Mjuumf Mbnc_
_Mary Had a Little Lamb_
Repeat? [y|n]: n
>>>
</code></pre>