• Nov 19, 2022 •CodeCatch
0 likes • 0 views
""" Rock Paper Scissors ---------------------------------------- """ import random import os import re os.system('cls' if os.name=='nt' else 'clear') while (1 < 2): print "\n" print "Rock, Paper, Scissors - Shoot!" userChoice = raw_input("Choose your weapon [R]ock], [P]aper, or [S]cissors: ") if not re.match("[SsRrPp]", userChoice): print "Please choose a letter:" print "[R]ock, [S]cissors or [P]aper." continue // Echo the user's choice print "You chose: " + userChoice choices = ['R', 'P', 'S'] opponenetChoice = random.choice(choices) print "I chose: " + opponenetChoice if opponenetChoice == str.upper(userChoice): print "Tie! " #if opponenetChoice == str("R") and str.upper(userChoice) == "P" elif opponenetChoice == 'R' and userChoice.upper() == 'S': print "Scissors beats rock, I win! " continue elif opponenetChoice == 'S' and userChoice.upper() == 'P': print "Scissors beats paper! I win! " continue elif opponenetChoice == 'P' and userChoice.upper() == 'R': print "Paper beat rock, I win! " continue else: print "You win!"
• Nov 18, 2022 •AustinLeath
0 likes • 5 views
mydict = {'carl':40, 'alan':2, 'bob':1, 'danny':0} # How to sort a dict by value Python 3> sort = {key:value for key, value in sorted(mydict.items(), key=lambda kv: (kv[1], kv[0]))} print(sort) # How to sort a dict by key Python 3> sort = {key:mydict[key] for key in sorted(mydict.keys())} print(sort)
0 likes • 9 views
#Python 3: Fibonacci series up to n def fib(n): a, b = 0, 1 while a < n: print(a, end=' ') a, b = b, a+b print() fib(1000)
0 likes • 3 views
# Python program for implementation of Bubble Sort def bubbleSort(arr): n = len(arr) # Traverse through all array elements for i in range(n-1): # range(n) also work but outer loop will repeat one time more than needed. # Last i elements are already in place for j in range(0, n-i-1): # traverse the array from 0 to n-i-1 # Swap if the element found is greater # than the next element if arr[j] > arr[j+1] : arr[j], arr[j+1] = arr[j+1], arr[j] # Driver code to test above arr = [64, 34, 25, 12, 22, 11, 90] bubbleSort(arr) print ("Sorted array is:") for i in range(len(arr)): print ("%d" %arr[i]),
• Mar 10, 2021 •Skrome
0 likes • 2 views
import string def caesar(text, shift, alphabets): def shift_alphabet(alphabet): return alphabet[shift:] + alphabet[:shift] shifted_alphabets = tuple(map(shift_alphabet, alphabets)) final_alphabet = "".join(alphabets) final_shifted_alphabet = "".join(shifted_alphabets) table = str.maketrans(final_alphabet, final_shifted_alphabet) return text.translate(table) plain_text = "Hey Skrome, welcome to CodeCatch" print(caesar(plain_text, 8, [string.ascii_lowercase, string.ascii_uppercase, string.punctuation]))
def get_ldap_user(member_cn, user, passwrd): ''' Get an LDAP user and return the SAMAccountName ''' #---- Setting up the Connection #account used for binding - Avoid putting these in version control bindDN = str(user) + "@unt.ad.unt.edu" bindPass = passwrd #set some tuneables for the LDAP library. ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW) #ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, CACERTFILE) conn = ldap.initialize('ldaps://unt.ad.unt.edu') conn.protocol_version = 3 conn.set_option(ldap.OPT_REFERRALS, 0) #authenticate the connection so that you can make additional queries try: result = conn.simple_bind_s(bindDN, bindPass) except ldap.INVALID_CREDENTIALS: result = "Invalid credentials for %s" % user sys.exit() #build query in the form of (uid=user) ldap_query = '(|(displayName=' + member_cn + ')(cn='+ member_cn + ')(name=' + member_cn + '))' ldap_info = conn.search_s('DC=unt,DC=ad,DC=unt,DC=edu', ldap.SCOPE_SUBTREE, filterstr=ldap_query) sAMAccountName = str(ldap_info[0][1]['sAMAccountName']).replace("[b'", "").replace("']","") return sAMAccountName