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
>>> range(10) | |
range(0, 10) | |
# Use list() to evaluate the range object | |
>>> list(range(10)) | |
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
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
def number_generator(): | |
i = 0 | |
while True: | |
yield i | |
i += 1 | |
numbers = number_generator() | |
next(numbers) # 0 | |
next(numbers) # 1 | |
next(numbers) # 2 | |
next(numbers) # 3 |
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
from itertools import count | |
numbers = count() | |
next(numbers) # 0 | |
next(numbers) # 1 | |
next(numbers) # 2 | |
next(numbers) # 3 | |
#... |
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
def tri_gen(): | |
tri_num = 0 | |
n = 1 | |
while True: | |
yield tri_num | |
tri_num += n | |
n += 1 | |
tri_numbers = tri_gen() | |
next(tri_numbers) # 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
(defn tri-gen | |
([] (tri-gen 0 1)) | |
([tri-num n] | |
(lazy-seq (cons tri-num | |
(tri-gen (+ tri-num n) (inc n)))))) | |
(def tri-numbers (tri-gen)) | |
(take 7 tri-numbers) | |
;; (0 1 3 6 10 15 21) |
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
(defn number-generator | |
([] (number-generator 0)) | |
([i] (lazy-seq (cons i | |
(number-generator (inc i)))))) | |
(def numbers (number-generator)) | |
(take 6 numbers) | |
;; (0 1 2 3 4 5) |
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
def is_tri(n): | |
for tri in tri_gen(): | |
if tri > n: | |
return False | |
elif tri == n: | |
return True | |
else: | |
continue | |
is_tri(10) |
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
(defn tri? | |
[n] | |
(= (last (take-while #(>= n %) tri-numbers)) n)) | |
(tri? 10) | |
;; true | |
(tri? 11) | |
;; false |
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
(def words | |
(clojure.string/split-lines (slurp "https://lamp.epfl.ch/files/content/sites/lamp/files/teaching/progfun/linuxwords.txt"))) | |
(def mnem | |
{\2 "ABC" \3 "DEF" \4 "GHI" \5 "JKL" \6 "MNO" \7 "PQRS" \8 "TUV" \9 "WXYZ"}) | |
(def char-code | |
(into {} (for [[k vs] mnem | |
v vs] | |
[v k]))) |
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
package week6 | |
import scala.io.Source | |
object mnemonics { | |
val in = Source.fromURL("https://raw.github.com/jpalmour/progfun/master/forcomp/src/main/resources/forcomp/linuxwords.txt") | |
val words = in.getLines.toList filter (word => word forall (chr => chr.isLetter)) | |
val mnem = Map( |