Giter Club home page Giter Club logo

coding-puzzles's Introduction

description

157. Read N Characters Given Read4

Given a file and assume that you can only read the file using a given method read4, implement a method to read n characters.

Method read4:

The API read4 reads four consecutive characters from file, then writes those characters into the buffer array buf4.

The return value is the number of actual characters read.

Solution:

Translating the description into English is the hardest part here (along with figuring out what buf4 does and how to use it). Essentially, we're given a method which can only read characters in groups of four and write them to some array we pass into it. The question is "how do we use a method that reads four things at a time to read any number of things?". Answer is simple:

  • Initialize a counter to keep track of how many characters we've read so far:
int readCount = 0;
  • For each group of four characters in n, initialize a buf4 array, read to it by calling read4(buf4), and store the number of actually read characters (which may be smaller than 4)
for (int i = 0; i < n; i+=4) {    
    char[] buf4 = new char[4]; 
    int read4Count = read4(buf4);
    //
}
  • Since we now have access to all the characters read to the buf4 array, all that's left is copying them over to our own buf array, making sure we stop copying if we've copied all the characters read to buf4 (read4Count) or enough total characters to reach n total characters (our target)
public int read(char[] buf, int n) {
    int readCount = 0;
    for (int i = 0; i < n; i+=4) {    
        char[] buf4 = new char[4]; 
        int read4Count = read4(buf4);
        for (int j = 0; j < read4Count && readCount < n; j++) {
            buf[i+j] = buf4[j];
            readCount++;
        }
    }
    return readCount;
}

coding-puzzles's People

Contributors

alesscif avatar gitbook-bot avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.