Problem of the Day
A new programming or logic puzzle every Mon-Fri

Digital Typewritter

We're going way back for this one. Today's problem is creating a digital typewritter. When the program is run it will allow the user to enter text. When the user hits enter that line will be saved to a text file. The program runs until the following combination occurs: blank line > enter > "." > enter.

Bonus Round

  • Open the file after the user is done inputting text (after the blank line > enter > "." > enter has occurred)
  • Limit the number of characters per line to 72

Permalink: http://problemotd.com/problem/digital-typewritter/

Comments:

  • Pegleg - 10 years, 8 months ago

    Good moring everyone, Python 3.4

    import webbrowser
    
    paper = open('Paper.txt','a+')
    blank_line = True
    period = True
    
    while period:
        user_input = input('Line check')
        if len(user_input)> 72:
            print('Error! Text fell off page TRUNCATED')
            user_input = user_input[:72]
        if not user_input:
            blank_line = False
        if not blank_line and user_input=='.':
            period = False
            break
        paper.write(user_input+'\n')
    
    webbrowser.open("Paper.txt")
    

    reply permalink

  • Anonymous - 10 years, 8 months ago

    blank_line should be checked and reset at the end of your loop, else the exit combination doesn't have to happen consecutively

    reply permalink

  • Hueho - 10 years, 8 months ago

    Dunno if the CLI open is avaiable anywhere outside Mac OS, but here we go:

    $file_name = ARGV.first
    
    FINISH = "."
    
    def read
      line = STDIN.gets(72)
      if line.strip.empty?
        read_next line
      else
        line
      end
    end
    
    def read_next(prev)
      result = prev
      loop do
        line = STDIN.gets(72)
        if line.strip.empty?
          result += line
        elsif line.strip == FINISH
          return nil
        else
          return result + line
        end
      end
    end
    
    while data = read
      open($file_name, "a") do |io|
        io << data
      end
    end
    
    system("open #{$file_name}")
    

    Also adding a byte limit in gets is not working for some reason. :(

    reply permalink

  • Walker Crouse - 10 years, 8 months ago

    It varies on different platforms, for instance Ubuntu and it's flavors use "see <file>".

    reply permalink

  • Walker Crouse - 10 years, 8 months ago

    Short but sweet Py

    from subprocess import call
    
    
    def addln(ln):
        with open("doc.out", 'a') as file:
            file.write(ln + "\n")
            file.close()
        return ln
    
    
    MAX_LN_LEN = 72
    
    
    def main():
        lines = []
        while True:
            lines_amt = len(lines)
            if lines_amt >= 3 and lines[lines_amt-1] == "" and lines[lines_amt-2] == "." and lines[lines_amt-3] == "":
                call("see doc.txt".split())
                break
            ln = raw_input(">: ")
            if len(ln) > MAX_LN_LEN:
                print("line too long (max " + str(MAX_LN_LEN) + ")")
                continue
            ln = "" if ln is None else ln
            lines.append(addln(ln))
    
    
    if __name__ == "__main__":
        main()
    

    Output

    reply permalink

Content curated by @MaxBurstein