I couldnt come up with a divide conqure algorithm My though
I couldn\'t come up with a divide & conqure algorithm. My thought for this problem is simply going through these n cards. We add to our dictionary (key = name, value = count) everytime we see a new name. If we see an already existed name, simply add count by 1. Does my proposed solution compute in O(n)? 2. What is the corresponding D&C solution?
Solution
A lexicon resembles a rundown, yet more broad. In a rundown, the lists must be numbers; in a lexicon they can be (nearly) any sort.
You can think about a word reference as a mapping between an arrangement of files (which are called keys) and an arrangement of qualities. Each key maps to an esteem. The relationship of a key and an esteem is known as a key-esteem combine or at times a thing.
For instance, we\'ll fabricate a lexicon that maps from English to Spanish words, so the keys and the qualities are all strings.
The capacity dict makes another word reference without any things. Since dict is the name of an implicit capacity, you ought to abstain from utilizing it as a variable name.
>>> eng2sp = dict()
>>> print eng2sp
{}
The squiggly-sections, {}, speak to a void word reference. To add things to the word reference, you can utilize square sections:
>>> eng2sp[\'one\'] = \"uno\"
This line makes a thing that maps from the key \"one\" to the esteem \'uno\'. In the event that we print the word reference once more, we see a key-esteem combine with a colon between the key and esteem:
>>> print eng2sp
{\'one\': \'uno\'}
This yield configuration is likewise an info arrange. For instance, you can make another word reference with three things:
>>> eng2sp = {\'one\': \'uno\', \'two\': \'dos\', \'three\': \'tres\'}
Be that as it may, in the event that you print eng2sp, you may be amazed:
>>> print eng2sp
{\'one\': \'uno\', \'three\': \'tres\', \'two\': \'dos\'}
The request of the key-esteem sets is not the same. Truth be told, in the event that you write a similar case on your PC, you may get an alternate outcome. When all is said in done, the request of things in a lexicon is eccentric.
In any case, that is not an issue in light of the fact that the components of a word reference are never listed with whole number lists. Rather, you utilize the keys to look into the comparing values:
>>> print eng2sp[\'two\']
\"dos\"
The key \"two\" dependably maps to the esteem \"dos\" so the request of the things doesn\'t make a difference.
In the event that the key isn\'t in the word reference, you get an exemption:
>>> print eng2sp[\'four\']
KeyError: \"four\"
The len work chips away at word references; it gives back the quantity of key-esteem sets:
>>> len(eng2sp)
3
The in administrator takes a shot at word references; it lets you know whether something shows up as a key in the lexicon (showing up as an esteem is sufficiently bad).
>>> \"one\" in eng2sp
Genuine
>>> \"uno\" in eng2sp
False
To see whether something shows up as an incentive in a lexicon, you can utilize the technique values, which gives back the qualities as a rundown, and afterward utilize the in administrator:
>>> vals = eng2sp.values()
>>> \"uno\" in vals
Genuine
The in administrator utilizes diverse calculations for records and lexicons. For records, it utilizes an inquiry calculation, as in Segment 8.6. As the rundown gets longer, the hunt time gets longer in direct extent. For lexicons, Python utilizes a calculation called a hashtable that has a surprising property: the in administrator takes about a similar measure of time regardless of what number of things there are in a word reference. I won\'t clarify how that is conceivable, yet you can read more about it at wikipedia.org/wiki/Hash_table.
Practice 1
Compose a capacity that peruses the words in words.txt and stores them as keys in a lexicon. It doesn\'t make a difference what the qualities are. At that point you can utilize the in administrator as a quick approach to check whether a string is in the word reference.
In the event that you exercised 10.8, you can contrast the speed of this usage and the rundown in administrator and the division seek.
11.1 Lexicon as an arrangement of counters
Assume you are given a string and you need to number how frequently each letter shows up. There are a few ways you could do it:
You could make 26 factors, one for each letter of the letter set. At that point you could cross the string and, for each character, increase the relating counter, most likely utilizing an anchored contingent.
You could make a rundown with 26 components. At that point you could change over each character to a number (utilizing the inherent capacity ord), utilize the number as a list into the rundown, and addition the suitable counter.
You could make a word reference with characters as keys and counters as the relating values. The first occasion when you see a character, you would add a thing to the word reference. After that you would increase the estimation of a current thing.
Each of these alternatives plays out a similar calculation, however each of them actualizes that calculation in an unexpected way.
An execution is a method for playing out a calculation; a few usage are superior to others. For instance, leverage of the word reference execution is that we don\'t need to know early which letters show up in the string and we just need to prepare for the letters that do show up.
Here is the thing that the code may resemble:
def histogram(s):
d = dict()
for c in s:
in the event that c not in d:
d[c] = 1
else:
d[c] += 1
return d
The name of the capacity is histogram, which is a factual term for an arrangement of counters (or frequencies).
The main line of the capacity makes a void word reference. The for circle crosses the string. Each time through the circle, if the character c is not in the word reference, we make another thing with key c and the underlying quality 1 (since we have seen this letter once). In the event that c is now in the word reference we increase d[c].
Here\'s the means by which it works:
>>> h = histogram(\'brontosaurus\')
>>> print h
{\'a\': 1, \'b\': 1, \'o\': 2, \'n\': 1, \'s\': 2, \'r\': 2, \'u\': 2, \'t\': 1}
The histogram demonstrates that the letters \"an\" and \"b\" show up once; \"o\" shows up twice, et cetera.


