Python Forum
feature request: ternary fallthroughs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
feature request: ternary fallthroughs
#12
(May-05-2021, 07:20 PM)rexrf Wrote: Im converting the int only once,
no you converting it twice
(May-05-2021, 07:02 PM)rexrf Wrote: val = int(sys.argv[1]) if int(sys.argv[1])

Just an example how your code may look
import sys
from string import ascii_lowercase

def read_file(file="/Users/rexfitzhugh/bin/data/words"):
    print("opening file..")
    with open(file) as f:
        contents = f.read().splitlines()
    print("contents stored...")
    return contents

def chek(word, ch_map, val):
    return sum(ch_map.get(ch, 0) for ch in word.lower()) == val

if __name__ == '__main__':
    try:
        val = int(sys.argv[1])
    except (ValueError, IndexError):
        print("expected a number as an arg")
    else:
        ch_map = {v:k for k, v in enumerate(ascii_lowercase, 1)}
        words = read_file() # instead of reading the whole file in memory you may just iterate over it line by line.
        matches = [word for word in words if check(word, ch_map, val)]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
feature request: ternary fallthroughs - by rexrf - May-05-2021, 12:57 PM
RE: feature request: ternary fallthroughs - by buran - May-05-2021, 07:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python on a ternary computer Skaperen 0 1,615 Jun-20-2020, 02:21 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020