Week 1
#2
-Building sets in math
S = {1, 3, 5, 7, 9, 11}
5 in S? True
6 in S? False
{1,3} in S? True
{1,4} in S? False
{x| x in S and x>6}
{7, 9, 11}
{x+2| x in S and x>6}
{9, 11, 13}
-Building sets in Python
S = {1, 3, 5, 7, 9, 11}
# Python doesn't look like this
{x for x in S and x>6}
| ∈
# Use these
# In python, elements do not reappear
{x for x in S if x>6}
{x+2 for x in S if x>6}
>>> Danny = {x for x in S if x>6}
>>> Danny
{9,11,17}
>>> any({False, False, False})
False
>>> any({True, False, False})
True
>>> all({False, True, True})
False
>>> all({True, True, True})
True
>> all({})
True
#3
def qo(S1, S2):
return not all({x in S2 for x in S1})
# Some element of S1 is not an element of S2.
def q1(S1, S2):
return any({x in S2 for x in S1})
# Some element of S1 is an element of S2
def q1(S1, S2):
return all({x in S2 for x in S1})
# Every element of S1 is an element of S2
# We include the situation that the set is an empty set
def q3(S1, S2):
return not any({x in S2 for x in S1})
# Every element of S1 is not an element of S2
# This also works when S1 is an empty set
Rigor without morits
1) Understand the problem
* Know what's given
* What's required
* Re-state the problem in your own words/ draw some diagrams
2) Plan solution(s)
* Try come up different approaches
3) Carry out your plan:
* WRITE IT DOWN
4) Review
Thoughts:
This week is the first week of university and our prof talked about sets.
And during the lecture, I found two things that are tricky, the 'for' and the duplicates.
-'for'
For example, in the lecture notes the python command Danny = {x for x in S if x > 6} is hard to understand and hard to precisely describe it in English. However, our professor suggested that we can understand it in the term of | of sets in maths. For example Danny = {x | x in S and x > 6}. In sets of maths, things before 'for' are the expression of element in the actual sets and things afterwards are the conditions that the item have to satisfy. So that {x | x in S and x > 6} means that for all the possible values of x in the universe, if that value is in S and it is greater than 6, then we append that value of x to the set. Thus we got {7, 9, 11} as the final result. And {x+2 | x in S and x > 6} means that for all the possible values of x in the universe, if that value is in S and it is greater than 6, then we append x+2 to the set. Thus we got {9, 11, 13}. We can apply similar idea to help us understanding 'for' in creating sets in Python. For instance, {x for x in S if x > 6} means that for all the xes in S, if that x is greater than 6, then append it to the set.
- duplicates
We know that sets have some wonderful features. One of them is it automatically merge all the duplicates. By my friend's Shrey's idea, we can write a python program with only one line to merge all the duplicates in a list or check if there is duplicates in the list. Here are the code.
def merge_duplicates(lst):
"""(list) -> list
return a list with all duplicates merged.
"""
return list(set(lst))
def contain_duplicates(lst):
"""(list) -> boolean
return True if there is no duplicate in the list given.
"""
return len(lst) == len(set(lst))
And this is very helpful in our CSC148 assignment.
No comments:
Post a Comment