Month: April 2018

The Pathfinder Graph Algorithm in Python

What is the output of this puzzle?   [python] n = 5 # The number of vertices in the graph # The graph represented as adjancy matrix # See https://en.wikipedia.org/wiki/Adjacency_matrix G = [[1,1,0,0,0], [0,1,0,0,0], [0,0,1,0,0], [0,1,1,1,0], [1,0,0,1,1] ] # Is there a path from vertex i to vertex j? def findPath(i,j,pathLength): # The maximal length …

The Pathfinder Graph Algorithm in Python Read More »

Dictionaries and Unpacking Arguments in Python

Which function correctly sorts the list?   [python] def f(val1=3, val2=4, val3=6): return val1 + val2 + val3 values = {"val1": 9, "val3":-1} print(f(**values)) [/python]   Programming is about using lower-level functionality to create higher-level functionality. In general, any programming language is a collection of functions that in turn build upon functions provided by the …

Dictionaries and Unpacking Arguments in Python Read More »

How to Modify a Sequence While Iterating over It (in Python)?

What is the output of this code snippet?   [python] words = [‘cat’, ‘mouse’, ‘dog’] for w in words[:]: if len(w) > 3: words.insert(0, w) print(words[0]) [/python]   How to modify a sequence while iterating over it? For example, you want to prepare a data set of house prices for a machine learning algorithm to …

How to Modify a Sequence While Iterating over It (in Python)? Read More »

Arbitrary Argument Lists, String Concatenation, and the Join Function in Python

What is the output of this code snippet?   [python] def concat(*args, sep="/"): return sep.join(args) print(concat("A", "B", "C", sep=",")) [/python]   String concatenation is the process of creating a string by appending string arguments. The given function takes an arbitrary number of string arguments as specified by the *args keyword. The parameter sep declares the …

Arbitrary Argument Lists, String Concatenation, and the Join Function in Python Read More »