This file contains hidden or 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
| function pairElement(str) { | |
| var unpaired = str.split(''); | |
| var newArr = []; | |
| function createPair(element){ | |
| switch(element) { | |
| case 'T': | |
| return(['T','A']) | |
| break; | |
| case 'A': |
This file contains hidden or 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
| function palindrome(str) { | |
| // Strip non-alphanumeric and convert to lowercase | |
| var string = str.replace(/[^0-9a-z]/gi, '').toLowerCase().split(""); | |
| // Iterate over half the number of chars in the string | |
| for(var i= 0; i < (string.length)/2; i++){ | |
| // Check i'th character from beginning and i'th character from end, accounting for 0. | |
| if(string[i] !== string[string.length-i-1]){ |
This file contains hidden or 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
| # Exercise 20 - Element Search from www.pythonpractice.org | |
| # Implement a binary search function. | |
| ordered_list = [1, 2, 5, 7, 9, 11, 15, 19, 20, 21, 40, 50] | |
| def binarysearch(ordered_list,low,high,x): | |
| if high >= low: | |
| mid = int(low + (high - low) / 2) | |
| if ordered_list[mid] == x: | |
| print("Found!") |
This file contains hidden or 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
| # Make a one-player Rock-Paper-Scissors game. | |
| # TODO: Abstract out Win, Lose, Draw conditions | |
| import random | |
| print("Lets play rock paper or scissors. It will be thrilling.\n") | |
| options = ["R","P","S"] | |
| # Choose random from options for computer's guess | |
| a = random.randint(0,2) |
This file contains hidden or 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
| # Given a list saved in a variable, write one line of Python that takes the list | |
| # and makes a new list that has only the even elements of this list in it. | |
| a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] | |
| # Place x'th value from a if that value has no remainder when / 2 (mod 2 = 0) | |
| b = [x for x in a if x % 2 == 0] |
This file contains hidden or 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
| # Ask the user for a string and print whether it is a palindrome or not | |
| # Get string | |
| # If the letters starting from the beginning of string are the same as from the end | |
| # Print string, say it's a palindrome | |
| # If the string is not a palindrome | |
| # Print string, say it's not a palindrome | |
| string = input("Enter a word: ") | |
| if string[:] == string [::-1]: |
This file contains hidden or 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
| # 1. Feed in two lists of different sizes - OK | |
| # 2. Return a list populated by common elements of the two lists - OK | |
| # 3a. Randomly generate two lists to test this | |
| # 3b. Write this is one line of Python | |
| a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] | |
| b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] | |
| c = [] | |
| for i in a: | |
| if i in b: |
This file contains hidden or 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
| # 1. Take a number DONE | |
| # 2. Print out a list of all the divisors of that number | |
| # For i in range that is less than _num_ | |
| # If number % i is 0 | |
| # append to x | |
| # Print the list | |
| num = int(input("Enter num: ")) | |
| x = [] |
This file contains hidden or 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
| # Take a list, say for example this one: | |
| # a = [1, 1, 2, 3, 5, 6, 13, 21, 34, 55, 89] | |
| # and write a program that prints out all the elements of the list that are less than 5 | |
| # Extras: | |
| # 1. Instead of printing the elements one by one make a new list that has all the elements less than 5 | |
| # from this list in it and print out this new list. | |
| # 2. Write this one line of Python. | |
| # 3. Ask the user for a number and return a list that contains only elements from the original list a | |
| # that are smaller than that number given by the user. |
NewerOlder