Skip to content

Instantly share code, notes, and snippets.

View jweinst1's full-sized avatar
🎯
Focusing

Josh Weinstein jweinst1

🎯
Focusing
View GitHub Profile
@jweinst1
jweinst1 / RecursionRangewithlist.py
Created July 7, 2015 07:37
RecursionRangewithlist.py
def addinglist(n):
if n == 7:
return [n]
return [n] + addinglist(n+1)
"""method of using recursion to construct a range list"""
@jweinst1
jweinst1 / GCD one liner.py
Created July 8, 2015 01:40
GCD one liner.py
def gcd(a, b):
return max([n for n in range(1, min(a, b)+1) if a%n == 0 and b%n == 0])
@jweinst1
jweinst1 / Shortening Search Algorithm.py
Created July 11, 2015 08:57
Shortening Search Algorithm.py
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()
@jweinst1
jweinst1 / Stringer Function
Created July 12, 2015 23:47
Function that returns a string of all the same 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
}
@jweinst1
jweinst1 / Check Final Letter
Created July 13, 2015 05:38
In Swift, this script uses the last() function to check if the inputted character is equal to the last character in a string.
var str = "Hello, playground"
func checklastletter(word: String, letter: String) ->String {
if letter == last(word) {
return true
} else {
return false
}
}
checklastletter(str, "g")
@jweinst1
jweinst1 / Removelastcharacter
Created July 13, 2015 18:33
Removes the last character in a string in Swift.
var str = "Hello, playground"
func removelastofstr(str: String) ->String {
var liststr = Array(str)
liststr.removeLast()
return String(liststr)
}
removelastofstr(str)
@jweinst1
jweinst1 / Get Character from StrIndexSwift
Created July 13, 2015 18:41
Get's the character at a specific index in a string in swift
var speech = "Hello, playground"
func indexstring(str: String, index: Int) ->Character {
var strlist = Array(str)
return strlist[index]
}
indexstring(speech, 0)
@jweinst1
jweinst1 / Conditional String Appending Swift
Created July 13, 2015 19:09
Function that can determine whether or not to add a character to a string based on some of the indexes of the string. Swift.
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
@jweinst1
jweinst1 / Reverse String Index Swift
Created July 13, 2015 19:27
In Swift, Takes a string and returns the nth character from the end, where 0 is the last character in the string.
// 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"
@jweinst1
jweinst1 / Swift select random element
Created July 14, 2015 00:52
Swift Method of selecting a random element from an array
let arras = ["Frodo", "sam", "wise", "gamgee"]
let randomIndex = Int(arc4random_uniform(UInt32(arras.count)))
var one = arras[randomIndex]