Created
May 14, 2013 05:40
-
-
Save jacks205/5573944 to your computer and use it in GitHub Desktop.
String Functions contains a group of random string methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate a string with all letters from the English alphabet. : | |
def alphabet(): | |
result = 'abcdefghijklmnopqrstuvwxyz'; | |
return result + result.upper(); | |
# Generate a randomly scrambled version of a string: | |
import random; | |
def scramble(string): | |
characterList = list(string); | |
random.shuffle(characterList); | |
return ''.join(characterList); | |
# Convert a floating point value to a string with specified decimal positions: | |
# Example: fpToString(value = 3.14159, decimalPositions = 2) returns '3.14'. | |
def fpToString(value, decimalPositions): | |
string = str(round(value, decimalPositions)); | |
periodPosition = string.find('.'); | |
if (periodPosition >= 0): | |
string = string[0: periodPosition + decimalPositions + 1]; | |
return string; | |
# Check if a word is a palindrome, i.e., reads the same backward: | |
def isPalindrome(word): | |
characterList = list(word); | |
characterList.reverse(); | |
backword = ''.join(characterList); | |
return (word == backword); | |
# Encode a message using a cipher, an 'old' to 'new' string mapping: | |
def encode(message, old, new): | |
result = []; | |
for char in message: | |
pos = old.find(char); | |
if(pos >= 0): | |
char = new[pos]; | |
result.append(char); | |
return ''.join(result); | |
# Decode a message using a given cipher: | |
def decode(message, old, new): | |
return encode(message, new, old); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment