# for loops 8 - Challenge¶

Browse file online

We now propose some exercises without solution, do you accept the challenge?

## Challenge - Faceborg¶

The social network Faceborg wants to assimilate your soul by ingesting your personal data. Every time you like or share a post, Faceborg knows it. Every time you see a like on a website, Faceborg knows you’ve been there, even without clicking the icon.

Faceborg already assimilated billions of people personal data, either because users explicitly inserted it into the system by completing their profiles, or because it was inferred by their online behaviours.

Faceborg wants even more, and sends you an automated email asking to improve its clustering algorithm to generate more revenue from advertisers. You firmly deny, to no avail: Faceborg puts a subliminal order into a flashing ad, and takes complete control of your mind.

Data model: A person in Faceborg is modelled as a dictionary holding fields of sensitive personal information. Field values are normalized in the range -1.0 to the opposite 1.0, for example a person might be represented as this:

person = {
'income'     : 0.9,
'health'     : 0.5,
'religiosity': -0.4,
'politics'   : -0.1
}


In order to perform fast clustering, Faceborg creates summaries of people dividing each field values into five categories:

-1.0-----1.0
12345

1: < -0.6
2: -0.6 <= x < -0.2
3: -0.2 <= x < 0.2
4: 0.2 <= x < 0.6
5: >= 0.6


For example, after being summerized, the person summary will look like this tuple:

>>> summary
(4, 5, 3, 2)


Notice labels in this representation are taken in sorted order, so the values above correspond to:

'health', 'income', 'politics', 'religiosity'


### Faceborg 1. Ingestion¶

Write some code that given a person dictionary produces a summary tuple using labels in sorted order.

[1]:


person = {
'income'     : 0.9,
'health'     : 0.5,
'religiosity': -0.4,
'politics'   : -0.1
}

# write here



### Faceborg 2. Selling your soul¶

The meaning of the various labels is stored in a dictionary which maps a field name to a two-elements tuple with the meanings of respectively the minimal (-1.0) and maximal value (1.0). NOTE: being negative or positive by itself has NO particular meaning:

labels = {
'income'     : ('poor','wealthy'),
'health'     : ('healthy', 'ill'),
'religiosity': ('religious', 'atheist'),
'politics'   : ('left', 'right')
}


Given a person summary as a tuple and a dictionary of labels, Faceborg wants to show advertisers the summary as a nice printout, so it commands you to write the code.

Example - given:

summary=(4, 5, 3, 2)
labels = {
'income'     : ('poor','wealthy'),
'health'     : ('healthy', 'ill'),
'religiosity': ('religious', 'atheist'),
'politics'   : ('left', 'right')
}


                       12345
health      :   healthy   * ill
income      :      poor    *wealthy
politics    :      left  *  right
religiosity : religious *   atheist


HINT: you may want to use methods str.ljust and str.rjust

[2]:


summary=(4, 5, 3, 2)
#summary=(1, 3, 4, 1)

labels = {
'income'     : ('poor','wealthy'),
'health'     : ('healthy', 'ill'),
'religiosity': ('religious', 'atheist'),
'politics'   : ('left', 'right')
}

# write here



### Faceborg 3. Go where the money is¶

Given a person summary, advertisers want to immediately know how many people there are for that particular summary.

Suppose Faceborg holds this current model for fast retrieval, which associates the tuples of person summaries to the number of people sharing that same summary:

model = {
(2, 1, 1, 3): 50000000,
(1, 3, 1, 2): 90000000,
(3, 3, 1, 2): 40000000,
(4, 5, 3, 2): 20000000,
(1, 3, 1, 1): 40000000,
(5, 3, 2, 3): 70000000,
(1, 3, 3, 2): 30000000,
}


Write some code that given a list of dictionaries people, updates the model with all their summaries.

Example - given:

people = [
{
'income'     : 0.9,
'health'     : 0.5,
'religiosity': -0.4,
'politics'   : -0.1
},  # corresponds to (4, 5, 3, 2), summary already present in the model
{
'income'     : 0.1,
'health'     : -0.6,
'religiosity': -0.8,
'politics'   : 0.5
} # corresponds to (2, 3, 4, 1), summary not present in the model
]


After your code, Faceborg model should become like this (order does not matter):

>>> model
{(1, 3, 1, 1): 40000000,
(1, 3, 1, 2): 90000000,
(1, 3, 3, 2): 30000000,
(2, 1, 1, 3): 50000000,
(2, 3, 4, 1): 1,
(3, 3, 1, 2): 40000000,
(4, 5, 3, 2): 20000001,  # note the 1
(5, 3, 2, 3): 70000000}

[7]:



model = {
(2, 1, 1, 3): 50000000,
(1, 3, 1, 2): 90000000,
(3, 3, 1, 2): 40000000,
(4, 5, 3, 2): 20000000,
(1, 3, 1, 1): 40000000,
(5, 3, 2, 3): 70000000,
(1, 3, 3, 2): 30000000,
}

people = [
{
'income'     : 0.9,
'health'     : 0.5,
'religiosity': -0.4,
'politics'   : -0.1
},  # corresponds to (4, 5, 3, 2), summary already present in the model
{
'income'     : 0.1,
'health'     : -0.6,
'religiosity': -0.8,
'politics'   : 0.5
} # corresponds to (2, 3, 4, 1), summary not present in the model
]

# write here



## Challenge - The informant¶

The FBI Crime unit is trying to infiltrate a powerful mob organization - so far, they managed to obtain the services of an informant who now and then gives precious tips. All the investigations are then recorded in classified documents, which need to be anonimized according to the reader’s clearence level. In order to mark possibly sensitive information, detectives place symbols such as > and < to delimit pieces of text to anonimize.

Write some code which anonimizes the text, substituting words in sensitive sequences with the proper number of asterisks.

Example - given:

text = """Our > informant Mr Big Ears <, who's operating within > the Organization, < told us
about a possible encounter among suspects we're following. The > suspect Mr Wrong Do
(also known as Mr Cut Throat) < was in fact seen last night near > Vice Palace < while
talking with > Mr So Bad <. They nervously glanced around, and > after a while,
they quickly exchanged two suitcases. < The details of the deal are yet to be discovered."""

punctuation = ['(',')','.',';',',']


Our  ********* ** *** **** , who's operating within  *** ************,  told
us about a possible encounter among suspects we're following. The  ******* **
***** ** (**** ***** ** ** *** ******)  was in fact seen last night near  ****
******  while talking with  ** ** *** . They nervously glanced around, and
***** * *****, **** ******* ********* *** *********.  The details of the deal
are yet to be discovered.


NOTE 1: Sometimes detectives place > < with punctuation around, your program should handle those cases as well, preserving punctuation in the output. I.e.

Our > informant Mr Big Ears <, who's

should become:

Our  ********* ** *** **** , who's

NOTE 2: Your program should also preserve punctuation in anonimized words, i.e.

suspect Mr Wrong Do (also known as Mr Cut Throat)

should become:

******* ** ***** ** (**** ***** ** ** *** ******)

HAVE YOU READ THE ABOVE NOTES?

Making a rough version of the program should be relatively straightforward, making the details also work may be more challenging

[1]:


text = """Our > informant Mr Big Ears <, who's operating within > the Organization, < told us
about a possible encounter among suspects we're following. The > suspect Mr Wrong Do
(also known as Mr Cut Throat) < was in fact seen last night near > Vice Palace < while
talking with > Mr So Bad <. They nervously glanced around, and > after a while,
they quickly exchanged two suitcases. < The details of the deal are yet to be discovered."""

punctuation = ['(',')','.',';',',']

# write here


[ ]: