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
def addinglist(n): | |
if n == 7: | |
return [n] | |
return [n] + addinglist(n+1) | |
"""method of using recursion to construct a range list""" |
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
def gcd(a, b): | |
return max([n for n in range(1, min(a, b)+1) if a%n == 0 and b%n == 0]) |
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
def Shortening_search(target): | |
assert 0<target<1000 #must be between 1 and 999. | |
list, count = range(1000), 0 | |
while len(list) > 1: | |
if list[0] == target: | |
return count | |
elif list[-1] == target: | |
return count | |
else: | |
list.pop() |
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
# simple stringer function using while loop | |
func stringer(char: String, len: Int) -> String { | |
var chain = char | |
while count(chain) < len { | |
chain += char | |
} | |
return chain | |
} |
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
var str = "Hello, playground" | |
func checklastletter(word: String, letter: String) ->String { | |
if letter == last(word) { | |
return true | |
} else { | |
return false | |
} | |
} | |
checklastletter(str, "g") |
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
var str = "Hello, playground" | |
func removelastofstr(str: String) ->String { | |
var liststr = Array(str) | |
liststr.removeLast() | |
return String(liststr) | |
} | |
removelastofstr(str) |
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
var speech = "Hello, playground" | |
func indexstring(str: String, index: Int) ->Character { | |
var strlist = Array(str) | |
return strlist[index] | |
} | |
indexstring(speech, 0) |
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
func indexstring(str: String, index: Int) ->Character { | |
var strlist = Array(str) | |
return strlist[index] | |
} | |
func addconorvow(str: String) ->String { | |
var modstr = str | |
if indexstring(str, 1) == "d" { | |
modstr += "d" | |
return modstr |
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
// Takes a string and returns the nth character from the end, where 0 is the last character in the string. | |
func reversestrindex(str: String, index: Int) ->Character { | |
var reversedstrArray = Array(reverse(str)) | |
return reversedstrArray[index] | |
} | |
reversestrindex("power", 2) | |
// "w" |
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
let arras = ["Frodo", "sam", "wise", "gamgee"] | |
let randomIndex = Int(arc4random_uniform(UInt32(arras.count))) | |
var one = arras[randomIndex] |
OlderNewer