Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Rodrick Brown Newsgroups: comp.lang.python Subject: Word Order Simple. Date: Fri, 11 Mar 2016 19:27:10 -0500 Lines: 74 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de BS77KCwuY/i61AgmFRkSxwKvWcRzR9+GUPULrt+1pbzQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'python3': 0.05; '__name__': 0.07; 'correspond': 0.07; 'lines.': 0.07; 'collections': 0.09; 'corresponds': 0.09; 'integer,': 0.09; 'lengths': 0.09; 'occurrences': 0.09; 'skip:0 70': 0.09; 'skip:1 70': 0.09; 'skip:u 60': 0.09; 'output': 0.13; 'weird': 0.15; "'__main__':": 0.16; '8bit%:1': 0.16; 'counter()': 0.16; 'defaultdict': 0.16; 'distinct': 0.16; 'input.': 0.16; 'lowercase': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:s 270': 0.16; 'skip:s 280': 0.16; '>': 0.18; 'input': 0.18; 'to:name:python-list@python.org': 0.20; 'correctly.': 0.22; 'skip:s 70': 0.22; 'appears': 0.23; 'seems': 0.23; 'second': 0.24; 'import': 0.24; 'words': 0.24; 'appear': 0.26; 'skip:# 10': 0.27; 'message-id:@mail.gmail.com': 0.27; '8bit%:3': 0.27; "skip:' 10": 0.28; '8bit%:2': 0.29; 'cat': 0.29; 'character.': 0.29; "i'm": 0.30; 'skip:a 70': 0.30; "can't": 0.32; 'skip:. 10': 0.32; 'maybe': 0.33; 'word.': 0.33; 'received:google.com': 0.35; 'next': 0.35; '8bit%:4': 0.35; 'appearance': 0.35; 'attempt': 0.35; 'skip:* 20': 0.35; 'according': 0.36; 'should': 0.36; 'there': 0.36; 'lines': 0.36; 'received:209.85': 0.36; 'cases': 0.36; 'to:addr:python-list': 0.36; 'seem': 0.37; 'received:209': 0.38; 'skip:s 40': 0.38; 'skip:p 20': 0.38; 'test': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'sample': 0.63; 'skip:a 40': 0.64; 'here': 0.66; 'letters': 0.67; 'sum': 0.69; 'exceed': 0.72; '*sample': 0.84; 'positions.': 0.84; 'repeat.': 0.84; 'skip:s 260': 0.84; 'ssm': 0.84; 'why?': 0.91; '8bit%:55': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=3B5VNiPHAWQGFXqpgGVWqV88FpA6k0J4OUb0tw13Ibo=; b=DYvk6UNVWihxcGJtmEm2GKW6AdbYYt36ZnZ5H+0JSxHpeeeydrQawUkEHGTufi9TmW eFMOQKEcyLqq8PGw6FGUjROszZXd/mTxr4Jx+Pi8XEnrKctkhkDPEvorjeOc9BfJ+8YF uveQbAvYjgSaTw3FFAm8MUovCS1DOhyVv8B1GDDynrb9afK0qdidsd8ymQSaqa5di2wI gatoxFiXdCAN1Zk3G6oMTkJUUMTbrig6aeyxE3SRgxqbrXbDy04gyehC368M5FB028xb KClxgUdbnXed6Q07gL1iWtIQ0fsnXUm3CIb5+IXHjG3+rCNLCZ+SsdPTJ46IJa6gTkPf yRRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=3B5VNiPHAWQGFXqpgGVWqV88FpA6k0J4OUb0tw13Ibo=; b=aUDhfzvqebB0gE8Pyu+azYjZFX2+ddcWPMGiiH/TgaMWFkuwdJyT/fywE1u7okr2HM Iztv45H1nAFPUdRz32qOv/9A1LTS1bTaTblwtbtgN/ltvJhy4VsX5Dbteeg9By+ViBaN x9XPbEcOxhENkXMIhh7IqwD0SSzBUqCDxUaxNxiuYan0TQhVWnPg/TnPz71VWAvMEERs ER6NZLVNUgaBedm+wjKnf5CiXL5OuZiY4P23UPs1VIQej8an5Myx1bmdT1Ya9+o6UBN3 Opj1lQ8/DOJ90NBVD99xRrQHTIi0DE08KWboJBVdI8D60UE/OrIOc5q4qavx0Pae+f6V DEJg== X-Gm-Message-State: AD7BkJIcm8MCK1jsBi5C8PhIUxix52Rr/ZRspoX5lPnioUq+FWhhtg9TchXdOnJ87ScSwy5YYLREfectA8aWzQ== X-Received: by 10.13.253.129 with SMTP id n123mr7304252ywf.64.1457742460313; Fri, 11 Mar 2016 16:27:40 -0800 (PST) X-Mailman-Approved-At: Sun, 13 Mar 2016 08:02:02 -0400 X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104758 You are given nn words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification. *Note:* Each input line ends with a *"\n"* character. *Constraints:* 1=E2=89=A4n=E2=89=A41051=E2=89=A4n=E2=89=A4105 The sum of the lengths of all the words do not exceed 106106 All the words are composed of lowercase English letters only. *Input Format* The first line contains the integer, nn. The next nn lines each contain a word. *Output Format* Output 22 lines. On the first line, output the number of distinct words from the input. On the second line, output the number of occurrences for each distinct word according to their appearance in the input. *Sample Input* 4 bcdef abcdefg bcde bcdef *Sample Output* 3 2 1 1 *Explanation* There are 3 distinct words. Here, *"bcdef"* appears twice in the input at the first and last positions. The other words appear once each. The order of the first appearances are *"bcdef"*,*"abcdefg"* and *"bcde"* which corresponds to the output. Here is my attempt I can't seem to past all test cases and not sure why? The explanation for line how to get 1 1 seems weird maybe I'm not reading it correctly. #!/usr/bin/env python3 from collections import defaultdict from collections import Counter if __name__ =3D=3D '__main__': words =3D defaultdict(list) for i,word in enumerate(input() for x in range(int(input()))): words[word].append([i+1]) count =3D Counter() print(len(words.keys())) for k in words: if len(words[k]) > 1: print(len(words[k]),end=3D' ') else: count[k] +=3D 1 for c in count.values(): print(c,end=3D' ') $ cat words.txt | ./wordcount.py 3 2 1 1 =E2=8F=8E