-3

Given a file of seemingly meaningless letters called “data.txt”, write a function findPin(inFile) that returns a four-number pin code within file. (All other characters are letters except for the pin code)

I know I'm supposed to open the data file and then iterate through it until I come across a number, but I'm not sure how to go about doing this. The data.txt file is...

loremipsumdolorsitametconsecteturadipiscingelit
seddoeiusmodtemporincididuntutlaboreetdoloremag
naaliquautenimadminimveniamquisnostrudexercitat
ionullamcolaborisnisiutaliquipexeacommodoconseq
uatduisauteiruredolorinrepre7269henderitinvolup
tatevelitessecillumdoloreeufugiatnullapariature
xcepteursintoccaecatcupidatatnonproidentsuntinc
ulpaquiofficiadeseruntmollitanimidestlaborumqwe

So obviously the pin number is 7269, but I need helping getting there. Sorry I'm just a beginner in python and this is really tripping me up.

  • you should use isdecimal() string method – mvelay May 11 '16 at 14:42
  • 1
    regex is the best method I believe – Alexander McFarlane May 11 '16 at 14:43
  • 1
    I have to point out though that is it a bit worrying that you are writing a function `findPin()` to extract a 4-digit PIN from an unknown and scrambled text-format... Whilst the solution for this is pretty easily obtainable it does beg to ask: *What are you actually doing here!?* - some more details given the possible context might be a good idea in future posts – Alexander McFarlane May 11 '16 at 15:36
  • I agree with @AlexanderMcFarlane's dup target but note that most of the answers on it assume that the number is separated to the other words by spaces, [this answer](http://stackoverflow.com/a/4289348/5827215) does show a version for both cases. – Tadhg McDonald-Jensen May 12 '16 at 16:10

3 Answers3

0

Use filter,

with open('data.txt', 'r') as f:
    pin_code = ''.join([filter(lambda x: x.isdigit(), line) for line in f])

print(pin_code)
# Output
7269

Or use re.sub,

import re

with open('data.txt', 'r') as f:
    pin_code = ''.join([re.sub("\D", "", line) for line in f])

print(pin_code)
# Output
7269
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134
0

regex is a more robust method than looping as providing different string formats can cause the loop to fail.

string = """loremipsumdolorsitametconsecteturadipiscingelit
seddoeiusmodtemporincididuntutlaboreetdoloremag
naaliquautenimadminimveniamquisnostrudexercitat
ionullamcolaborisnisiutaliquipexeacommodoconseq
uatduisauteiruredolorinrepre7269henderitinvolup
tatevelitessecillumdoloreeufugiatnullapariature
xcepteursintoccaecatcupidatatnonproidentsuntinc
ulpaquiofficiadeseruntmollitanimidestlaborumqwe"""

This is using regex

import re
results = re.findall(r"\d+", string)

This provides a list of each digit instance which can be merged by: merged = "".join(result)

Here is a link to a site that demonstrates some common uses of regex pattern matching for your future use.

Few people remember regex patterns but most programmers will know when to use them and know how to search for the correct pattern quickly.

Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100
-3
a = 'abc1234def'
for i in range(len(a)):
    if a[i].isdigit():
        print a[i:i+4]

Updated with hints from comments.

Dschoni
  • 3,714
  • 6
  • 45
  • 80
  • Actually this really does not work, at all. – Ajean May 11 '16 at 18:32
  • 1
    good attempt, although you need `a[i].isdigit()` for it to work correctly and `a[i:i+4]` to correctly use slicing (what you have works fine but I'd recommend doing the slice in one step if you can) – Tadhg McDonald-Jensen May 12 '16 at 16:04
  • I just now realized, that this is stupid. It will print '1234' as well as '234d', '34de' and '4def'. – Dschoni May 17 '16 at 10:37