Loading...
More Python Posts
from collections import defaultdictdef combine_values(*dicts):res = defaultdict(list)for d in dicts:for key in d:res[key].append(d[key])return dict(res)d1 = {'a': 1, 'b': 'foo', 'c': 400}d2 = {'a': 3, 'b': 200, 'd': 400}combine_values(d1, d2) # {'a': [1, 3], 'b': ['foo', 200], 'c': [400], 'd': [400]}
# function which return reverse of a stringdef isPalindrome(s):return s == s[::-1]# Driver codes = "malayalam"ans = isPalindrome(s)if ans:print("Yes")else:print("No")
# Python code to demonstrate# method to remove i'th character# Naive Method# Initializing Stringtest_str = "CodeCatch"# Printing original stringprint ("The original string is : " + test_str)# Removing char at pos 3# using loopnew_str = ""for i in range(len(test_str)):if i != 2:new_str = new_str + test_str[i]# Printing string after removalprint ("The string after removal of i'th character : " + new_str)
import copybegining = [False,False,False,False,False,None,True,True,True,True,True]#False = black True = whiteits = [0]def swap(layout, step):layoutCopy = copy.deepcopy(layout)layoutCopy[(step[0]+step[1])], layoutCopy[step[1]] = layoutCopy[step[1]], layoutCopy[(step[0]+step[1])]return layoutCopydef isSolved(layout):for i in range(len(layout)):if(layout[i] == False):return (i >= (len(layout)/2))def recurse(layout, its, steps = []):if isSolved(layout):its[0] += 1print(layout,list(x[0] for x in steps))returnstep = Nonefor i in range(len(layout)):if(layout[i] == None):if(i >= 1): #If the empty space could have something to the leftif(layout[i - 1] == False):step = [-1,i]recurse(swap(layout,step), its, (steps+[step]))if(i > 1): #If the empty space could have something 2 to the leftif(layout[i - 2] == False):step = [-2,i]recurse(swap(layout,step), its, (steps+[step]))if(i < (len(layout)-1)): #If the empty space could have something to the rightif(layout[i + 1] == True):step = [1,i]recurse(swap(layout,step), its, (steps+[step]))if(i < (len(layout)-2)): #If the empty space could have something to the rightif(layout[i + 2] == True):step = [2,i]recurse(swap(layout,step), its, (steps+[step]))its[0] += 1#return Nonerecurse(begining,its,[])print(its[0])
""" Rock Paper Scissors----------------------------------------"""import randomimport osimport reos.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 choiceprint "You chose: " + userChoicechoices = ['R', 'P', 'S']opponenetChoice = random.choice(choices)print "I chose: " + opponenetChoiceif 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! "continueelif opponenetChoice == 'S' and userChoice.upper() == 'P':print "Scissors beats paper! I win! "continueelif opponenetChoice == 'P' and userChoice.upper() == 'R':print "Paper beat rock, I win! "continueelse:print "You win!"
prime_lists=[] # a list to store the prime numbersdef prime(n): # define prime numbersif n <= 1:return False# divide n by 2... up to n-1for i in range(2, n):if n % i == 0: # the remainder should'nt be a 0return Falseelse:prime_lists.append(n)return Truefor n in range(30,1000): # calling function and passing starting point =30 coz we need primes >30prime(n)check=0 # a var to limit the output to 10 onlyfor n in prime_lists:for x in prime_lists:val= n *xif (val > 1000 ):check=check +1if (check <10) :print("the num is:", val , "=",n , "* ", x )break