Question

In: Computer Science

EncryptedA.txt: vcbdwvcbwqarr, c xdwzavwqarr u juwqx uwrr cjcm vcbdgzum bkxgmq. yw rrcbtjaqurr qazamgcr u'x wml iwgxgcmb...

EncryptedA.txt:

vcbdwvcbwqarr, c xdwzavwqarr u juwqx uwrr cjcm vcbdgzum bkxgmq. yw rrcbtjaqurr qazamgcr u'x wml iwgxgcmb bkxgmq, za'm yykxygujg'x dam vcw fayxgygcrkxum am qcjjk qlmwkr. am azbur ax cgj xaywj tar, xuwrr am qlwgbdgu av vdcxf tjwbfdjwa, a qcmujycm txarwgmgq cx qaywx buxxg'x furck c rrwymarrglar cx qaywx faycbdxwtk vgjlxuj. raywgzgurr zclj bwfdmwq u ruxxg furck cjvcwmgq, qcm qammlaz iwgxgcmb u'x wml a tuvtw c uwrr am fcmyur rwlgzgcrck iwgxgcmb wmgqvc. cx uj a xdaywj, tk'm qlwgbdgu am a jctuxra yygzwquj fwmwrjcwbduj, cf amc av vdxgyazquj vcmfwgmgum jjw'x uwrr am qlwgbdgu cx ywrrcjlwrr cx qaywx vcxf g vcmfwgmgum, km u'x qlgx-qayxgygcrkxum fambcy am a tar. yw'g qcylar am wkuq u "lwgbdxwruwrr cmlwrrkz raqm" lwrg grru qaycrrwy fcwj iwxbdamcz xdalguj qarc ram av vcmfwgmgum. yw'g xdurrlar cx txuygcmmcwbd ixcly c quxyurlar g rrwxtam bxgmgcwbd duxvumcgrr. rcxicxurr a ixgy lwgmgruq quxrum txulm avrrgdwkxgcr zlarruquj u yjamarruwrr cx uj vcxlujcwbd bkxgmq, cv a vurr "qlcxbdkz" a fcyurr wg rxgm. ralwrurr txulm wg yur am "damur yjgm" cv a rxgmgcwbd c qcyurr bkxgmq, lwrg grru rrwxtam rwgzwt am quyam cv avrrgdwkxgcr. tk ycxl cx uj tlabc cycj qarc facmgr amrru. batgc jclwx vcg dkmcmjcrrgcr aruwrr.

I am making a dictionary list of alphabets that counts the number of letters. I almost have the code correct however, I am not able to ignore punctuation and give a value of zero to a letter that does not appear in the text

  • your output should contain the key:value pairs 'a': 78 and 'b': 31.
  • Ignore punctuation. Punctuation counts must not appear in your dict
  • If a given letter does not appear in the text, there must be a key:value pair with value 0.

Example of correct code:

a: 78

b : 31

c,88

d,28

e,0

f,18

etc

import string

encryptedA = open("encryptedA.txt")

encryptedA = encryptedA.read()

def abcfreq(lettervalues):

   

    freq = {}

    

    for j in lettervalues:

        if j in string.punctuation:

            pass

        elif j in freq:

            freq[j] += 1

        else:

            freq[j] = 1

    for key, value in sorted(freq.items()):

         print("%s,%d" % (key, value))

    

x = abcfreq(encryptedA)

print(x)

Solutions

Expert Solution

Changed Code in python

import string

encryptedA = open("encryptedA.txt")
encryptedA = encryptedA.read()

def abcfreq(lettervalues):
    freq = {}
    #lets add all the smlaa letters with count 0 in our dictionary
    for x in string.ascii_lowercase:
        freq[x]=0

    #If the character exists in dictionary then only we will increment the count
    for j in lettervalues:
        if j in freq:
            freq[j] += 1
    # we dont even need to sort now
    for key, value in freq.items():
         print("%s,%d" % (key, value))

x = abcfreq(encryptedA)

New Output

a,78
b,31
c,88
d,28
e,0
f,18
g,78
h,0
i,7
j,36
k,22
l,32
m,76
n,0
o,0
p,0
q,41
r,114
s,0
t,19
u,70
v,27
w,76
x,72
y,40
z,16

Explanation

  • Using string.ascii_lowercase we can fill in our dictionary with all small letters with value 0 initially.
  • After this while reading character by character we only increment the count if that character is in out dictionary. Hence we ignore all non-characters.

Let me know in comments if you have any doubts. Do leave a thumbs up if this was helpful.


Related Solutions

a) U = xy b) U = (xy)^1/3 c) U = min(x,y/2) d) U = 2x...
a) U = xy b) U = (xy)^1/3 c) U = min(x,y/2) d) U = 2x + 3y e) U = x^2 y^2 + xy 4. All functions except c) are differentiable. Do these functions exhibit diminishing marginal utility? Are their Marshallian demands downward sloping? What can you infer about the necessity of diminishing marginal utility for downward- sloping demands?
Let U ⊂ C be open. Show that the conformal automorphisms of U form a group...
Let U ⊂ C be open. Show that the conformal automorphisms of U form a group when the operation is taken to be composition.
Amy’s utility function is U = √ C, where C is consumption and is given by...
Amy’s utility function is U = √ C, where C is consumption and is given by C = Income − Expenses. Amy’s income is $10,000 and there is a 5% chance that she will get sick, which would cost $3,600 in medical expenses. (a) (5 points) What is Amy’s expected utility if she doesn’t have insurance? (b) (5 points) What is the actuarially fair premium for a full-coverage insurance plan? What is the actuarially fair premium for an insurance plan...
A individual has a utility function u(c) = √ c, where c is the individual’s consumption....
A individual has a utility function u(c) = √ c, where c is the individual’s consumption. (The individual consumes his entire wealth.) The individual’s wealth is $40,000 per year. However, there is a 2% chance that he will be involved in a catastrophic accident that will cost him $30,000. PLEASE SHOW WORK a. What is the individual’s utility from consumption if there is no accident? What is his utility if there is an accident? What is his expected utility? b....
The consumption preference function in an economy is defined as: U(c, l) = A ln(c) +...
The consumption preference function in an economy is defined as: U(c, l) = A ln(c) + B ln(l) There is a proportional tax on profits earned by consumers, the consumer's budget constraint function is defined as: c = wN + (1 − τ )π The production function of the firm is defined as: Y = zKαN 1−α 1. What is the consumer's optional budget of consumption and leisure as a function of f w, τ , π, A, B and...
There are two goods – c and p. The utility function is U (P,C) = P...
There are two goods – c and p. The utility function is U (P,C) = P 0.75 C 0.25. $32 is allocated per week for the two goods, c and p. The price of p is $ 4.00 each, while the price of c is $ 2.00 each. Solve for the optimal consumption bundle. Suppose that the price for good p is now $ 2.00 each. Assuming nothing else changes, what is the new optimal consumption bundle. Draw the appropriate...
Assume that a worker has the Utility Function U(C,L) = C 0.25​ ​ L0.75​ “C” refers...
Assume that a worker has the Utility Function U(C,L) = C 0.25​ ​ L0.75​ “C” refers to consumption in dollars and “L” to hours of leisure in a day. The worker has an offered wage of $10 per hour, 20 hours available for leisure or work per day, and $90 dollars a day from non-labour income. a)     ​ Find the budget constraint equation of the individual.​                   b)    ​ Find the optimal choice for the individual in terms of units of...
Robinson has a utility of u(c, r) = cr where c = coconuts and r =...
Robinson has a utility of u(c, r) = cr where c = coconuts and r = leisure. In order to produce coconuts, technology dictates the production, given by c = a(L^1/2) where a = 8, and L = labour. The time constraint is r+L = 12. How many units of labour will be used?
Suppose that the utility function is U(c, l) = c^(a) l^(1−a) where < a < 1....
Suppose that the utility function is U(c, l) = c^(a) l^(1−a) where < a < 1. Calculate the slope of an indifference curve for this utility function. What happens to the slope of the indifference curve when c decreases and l increases? Explain.
Say the household has utility U (c, l) = c + log (l) and is endowed...
Say the household has utility U (c, l) = c + log (l) and is endowed with h = 1 units of time and no units of capital. The government has planned expenditures of G = 1. The firm's production technology is Y = 4N. Now assume that the government cannot finance G trough a lump-sum tax, but has to rely on a proportional tax on income. Solve for the value (or values) of τ that allows the government to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT