diff --git a/src/extra_interview_problems/basic_examples/example_comp_lists.py b/src/extra_interview_problems/basic_examples/example_comp_lists.py new file mode 100644 index 0000000..cb1fd16 --- /dev/null +++ b/src/extra_interview_problems/basic_examples/example_comp_lists.py @@ -0,0 +1,26 @@ +Bad: + +# Filter elements greater than 4 +a = [3, 4, 5] +b = [] +for i in a: + if i > 4: + b.append(i) +Good: + +a = [3, 4, 5] +b = [i for i in a if i > 4] +# Or: +b = filter(lambda x: x > 4, a) +Bad: + +# Add three to all list members. +a = [3, 4, 5] +for i in range(len(a)): + a[i] += 3 +Good: + +a = [3, 4, 5] +a = [i + 3 for i in a] +# Or: +a = map(lambda i: i + 3, a) \ No newline at end of file diff --git a/src/extra_interview_problems/basic_examples/string_format.py b/src/extra_interview_problems/basic_examples/string_format.py new file mode 100644 index 0000000..a5c03b6 --- /dev/null +++ b/src/extra_interview_problems/basic_examples/string_format.py @@ -0,0 +1,6 @@ +foo = 'foo' +bar = 'bar' + +print '%s%s' % (foo, bar) # It is OK +print '{0}{1}'.format(foo, bar) # It is better +print '{foo}{bar}'.format(foo=foo, bar=bar) # It is best \ No newline at end of file