siteIcon

Tech Novice Tools

Python Source Code

burgerIcon

Jumbalayapuzzle

Python3 Source Code

Jumbalaya was originally re-vamped in 2019 in Python3 (after earlier renditions in Java), using the Eclipse IDE. (We have also used Pythonista on the iPad as a GREAT coding environment).

The JavaScript version was created by translating the Python3 code into JavaScript using help from Google, especially W3Schools.

It was considerably easier to write in Python3!

Jumbalaya Eclipse Source Code
                            
#--- Header Comments ---
#Date: 9/11/19
#Coder: 
#Path: 
#File: 
#Purpose: 
#Notes: 
#https://www.mnn.com/lifestyle/arts-culture
/stories/why-your-
brain-can-read-jumbled-letters
#TODO: 

#--- Imports ---
import datetime #for current time
import getpass  #for username
import os       #for current path
import sys      #for versioning
import random



#--- Global Variables ---

#--- Functions ---
def print_current_user():
    username = getpass.getuser()
    print('Coder: ' + username)
    #https://stackoverflow.com/questions
    /5137497/find-current-directory-and-files-directory
    dir_path = os.path.dirname(os.path.realpath(__file__))
    print(dir_path)
#end print_current_user function
    
def print_current_time():
    now = datetime.datetime.now()
    print(now)
#end print_current_time function
    
def print_simple_heading(heading, symbol, n):
    my_decoration = ''
    for _ in range(n):
        my_decoration += symbol
    my_heading = my_decoration + heading + my_decoration;
    print(my_heading)
#end print_simple_heading function

def make_string_from_char(ch, length):
    temp = ''
    for _ in range(length):
        temp += ch
    return temp
#end make_string_from_char function

#utilize default parameter values
def intro(title="Python App", stageNo='1', explanation=''):
    num_char = 5
    my_char = '='
    title = title + ": Stage " + str(stageNo)
    print_simple_heading(title, my_char, num_char)
    print_current_user()
    print_current_time()
    print("Version:", sys.version)
    if len(explanation) > 0:
        print("")
        print(explanation)
    border_length = len(title) + 2*num_char;
    border = make_string_from_char(my_char, border_length)
    print(border)  
#end intro function

#---My Functions---
#https://stackoverflow.com/questions
/20601480/randomize-letters-in-a-word

def jumbleA(phrase):
    original = phrase
    lcPhrase = phrase.lower()
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    ucAlphabet = alphabet.upper()
    #create a punctuation vector
    punctuationList = []
    lettersAndSpacesOnly = ""
    for symbol in original:
        if symbol in alphabet or symbol in ucAlphabet:
            punctuationList.append('x')
            lettersAndSpacesOnly += symbol
        else:
            if symbol == " ":
                lettersAndSpacesOnly += symbol
            punctuationList.append(symbol)
            
    wordsList = lettersAndSpacesOnly.split(" ")
    jumbledPhraseList = []
    for word in wordsList:
        wrd = reallyJumble(word)
        jumbledPhraseList.append(wrd)
    
    jumbledPhrase = ' '.join(jumbledPhraseList)
    ndx = -1
    for symbol in punctuationList:
        ndx += 1
        if symbol != 'x' and symbol != " ":
            #https://stackoverflow.com/questions
            /36050848/inserting-characters-in-strings-in-python?rq=1
            jumbledPhrase = jumbledPhrase[:ndx] + 
            symbol + jumbledPhrase[ndx:]
        
    return jumbledPhrase
            
#end function jumbleA

def jumble(word):
    #jumble the middle, but letters can be in their original locations
    original = word
    length = len(word)
    if length <= 3:
        return word
    else:
        word = word.lower()
        firstLetter = word[0:1]
        lastLetter = word[length-1: length]
        middle = word[1:length-1]
        
        #convert the string into a list of characters
        charlst = list(middle) 
        # shuffle the list of characters randomly
        random.shuffle(charlst)
        #convert the list of characters back into a string
        jumbledMiddle = ''.join(charlst) 
         
        temp = firstLetter + jumbledMiddle + lastLetter
        temp = matchCaseTo(original, temp)
        return temp
#end function jumble

def reallyJumble(word):
    #jumble the middle, 
    #try to force innerds to be different from original
    original = word
    length = len(word)
    if length <= 3:
        return word
    else:
        word = word.lower()
        firstLetter = word[0:1]
        lastLetter = word[length-1: length]
        middle = word[1:length-1]
        
        attempts = 0
        maxAttempts = 10
        jumbledMiddle = ""
        
        while attempts <= maxAttempts or middle == jumbledMiddle:
            attempts += 1
            #convert the string into a list of characters
            charlst = list(middle)  
            #shuffle the list of characters randomly
            random.shuffle(charlst)
            #convert the list of characters back into a string
            jumbledMiddle = ''.join(charlst) 
         
        temp = firstLetter + jumbledMiddle + lastLetter
        temp = matchCaseTo(original, temp)
        return temp
#end function jumble

def matchCaseTo(original, newbie):
    temp = ""
    locn = 0
    for symbol in original:
        #ndx = original.find(symbol)
        if symbol.isupper():
            ltr = newbie[locn:locn+1]
            ltr = ltr.upper()
            temp += ltr        
        else:
            temp += newbie[locn: locn + 1]
        locn += 1
    
    return temp
#end function matchCaseTo

#--- Driver Code ---
def main():
    explanation = 'Jumble the middle of a word or phrase, 
    preserving case and punctuation'
    intro(title="Jumbled Middle App", stageNo=4, explanation=explanation)
    print("Jumbled Middle App")
    #phrase = "When you're coding, it's a very HAPPY day!"
    phrase = "In brightest day, in blackest night; 
    no evil shall escape my sight. 
    Let those who worship evil's might, 
    beware my power: Green Lantern's LIGHT!"
    print(phrase)
    jumbledPhrase = jumbleA(phrase)
    print(jumbledPhrase)
#end function main

if __name__ == '__main__':
    main()
#end conditional

'''
    =====Jumbled Middle App: Stage 4=====
Coder: kp
/Users/kp/Desktop/2019-2020_Devmt/kp19-HCSF-T1/Demos
/AlienLanguageSagaWS/JumbledMiddlePrj-S4
2019-09-11 08:45:04.998439
Version: 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) 
[Clang 6.0 (clang-600.0.57)]

Jumble the middle of a word or phrase, preserving case and punctuation
=====================================
Jumbled Middle App
In brightest day, in blackest night; no evil shall escape my sight. 
Let those who worship evil's might, beware my power: 
Green Lantern's LIGHT!
In bsegrhitt day, in bseakclt nhgit; no eivl slahl epacse my shgit. 
Let tsohe who woihrsp elvi's mhgit, bwraee my pweor: 
Geren Lantner's LHIGT!
''                        

Last update: 09/17/19