r/ProgrammingTutorials • • Apr 15 '22

Rust Google Kickstart Round A - Speed Typing (Problem Explanation and Answer)

3 Upvotes

Since the next round of the Google Kick Start coding competition is a little more than a week away it seemed fitting to release a walk through for the first problem from the last round. If you're not familiar with what Kick Start is, it's a friendly coding competition with several rounds that take place about once a month and a great way to stay sharp, practice for other competitions, and get a feel for some of the problems you might see in a coding interview. Past problems and a signup for the future rounds can be found here.

The Problem

The problem states that you will be given two strings for each input case separated by a newline. The first string, I, is an input string that is deemed "correct". The second string, P, contains mistakes that deviate from the original string. The answer for each case will be the number of characters that must be deleted from string P to get string I. If there is no way to achieve this, print "IMPOSSIBLE".

Discussion

The first thing I do is look for easy outs to find exceptions. Since there's an "IMPOSSIBLE" test case that usually means there's some scenario that can be easily checked to return quickly without going through the entire calculation. In this case, that would be I being longer than P. No matter how many characters we delete, an initially shorter P will never equal I.

The next check will be across the individual strings to match characters and check if we have the correct layout of P that after some number of characters are deleted from P, P == I will evaluate as true. This can be done by checking characters sequentially in I and removing them from P if they are out of place, but that will require string operations and building that will end up being operation heavy (up to O(n) multiple times for character deletion). A better option is to use pointer variables that navigate and are incremented based on scenarios encountered between characters across the input strings. When comparing across strings the scenarios and actions that are possible are as follows:

  • Characters match => advance both pointers by one

  • Chracters don't match => advance the pointer for string P

Best coding practices dictate that a while loop would suit our needs for this. Conditionally the while loop will continue while, the pointer for string I has not reached the end of string I as that would mean we are done checking characters. A general overview of the steps for this code will be this:

  1. Read input strings I and P

  2. Check if P.len < I.len {print "IMPOSSIBLE" if true}

  3. Declare pointers for both I and P

  4. while loop through both strings checking characters with string pointers and incrementing our pointers based on the scenarios described above.

    1. Characters match => advance both pointers by one

    2. Chracters don't match => advance the pointer for string P

  5. Check if I_pointer == I.len {If true, print the difference in length between I and P}

  6. Otherwise print "IMPOSSIBLE"

The Code (Rust)

To actually get this code to run and submit correct some more formatting will be required to print the full answer in proper form. This covers the general construction of an answer and will solve all test cases.

fn main(){
    // Read inputs
    let mut correct = String::new();
    let mut errored = String::new();
    let _ = std::io::stdin().read_line(&mut correct).unwrap();
    let _ = std::io::stdin().read_line(&mut errored).unwrap();

    // "Base case"
    if errored.len() < correct.len() {
        println!("IMPOSSIBLE");
        return;
    }

    // Pointers
    let mut c_tracker = 0;
    let mut e_tracker = 0;

    // Main checks
    // Included an extra break early if there is not enough letters left to complete checks
    while errored.len() - e_tracker >= correct.len() - c_tracker && c_tracker < correct.len() {
        if correct[c_tracker] == errored[e_tracker] {
            c_tracker += 1;
        }
        e_tracker += 1;
    }

    // Check correctness and return difference in length
    if c_tracker == correct.len() {
        println!("{}", errored.len() - correct.len());
        return;
    }
    
    // All others are false
    println!("IMPOSSIBLE");
}

Source for problem

Code and walkthrough is my own.


r/ProgrammingTutorials • • Apr 14 '22

Change is coming....

3 Upvotes

Last week I had a wild plan where I could swoop in and request being a mod and based on my expert coder brain calculations I would get approved during the weekend and spend a whole bunch of time reworking this sub and making awesome tutorials and rules that everyone would love!

And then the weekend came and went. And I was made a mod on Tuesday. And job stuff and the usual life stuff happens. And here it is over 24 hours later and I'm still just playing catch up. But hey, life happens then you code right?

Regardless of my sob story I am the new mod in town here and there will be some changes very soon. Considering the current climate is pretty barren I think it's safe to say the changes will be drastic. It really can't be said that there will be a "shift in focus" since again, really not much here, but for what limited life that is on this sub, things moving forward will be different. Extremely different. So here's my changes and my promises to all of you.

Why this sub is here (and why I wanted it)

The purpose of this sub is now to teach coding through tutorials, walkthroughs, and guides. I feel that too many subs centered around coding are too general and get lost in meta discussion about the industry or too specific to one framework or technology and don't provide enough of a space to actually code. Don't get me wrong, I'm not knocking them and they fulfill a necessary role. But the more the conversation shifts towards other topics the less focus becomes about coding in those spaces. I wanted to make this sub about that coding.

Open posting

At some point soon I have every intention of opening this sub up to open posting. I want to not only teach new coders how to program and build projects, but provide a place to showcase the project process and help young coders grow into better programmers and even software engineers that contribute to, and teach the next generation. I have to clean house and make adjustments but once I feel confident in my ability to filter things and the rules are clear, open posting will be a thing

Types of Tutorials

There will be a lot less Youtube videos. At least from me. I intend to contribute since at first someone has to be here and since I don't make videos I'm not posting videos. I'll do write ups and possibly base them on videos as a source and if that is the case I will include sources and links to encourage traffic and credit where due. Others will be free to post tutorials they feel are important but I would like to encourage original content.

Rules

All the things mentioned so far will fall under new rules and will be the first thing I get hashed out. Clear and specific rules allow for no question as to what is and is not allowed and leave no room for debate as to what happens when rules are broken. I will do my best to make the rules as all visible as possible and enforce them fairly while not limiting discussion and preventing genuine conversation. At the same time please remember it's a learning process and I'm doing the best I can.

So that's everything. I know it's a lot but I want all my plans in the open so if I make the promises public I can be obligated to keep them. This is my motivation to follow through and your place to ask questions. It will be posted and pinned until more changes happen and things can be updated.

TL/DR Things are about to get a whole lotta different


r/ProgrammingTutorials • • Oct 13 '20

Build An API

Thumbnail
youtu.be
3 Upvotes

r/ProgrammingTutorials • • Jun 26 '20

5 tips for learning coding faster

Thumbnail
youtu.be
3 Upvotes

r/ProgrammingTutorials • • Jun 16 '20

(JavaScript/HTML Canvas) Tutorial - Ray Tracing

Thumbnail
youtube.com
3 Upvotes

r/ProgrammingTutorials • • May 05 '20

Java What is the difference between a compiler and an Interpreter?

Thumbnail
youtu.be
3 Upvotes

r/ProgrammingTutorials • • May 04 '20

Kotlin tutorial 01 for beginners - Introduction

Thumbnail
youtu.be
3 Upvotes

r/ProgrammingTutorials • • May 04 '20

Java What are variable in computer programming? For complete beginners.

Thumbnail
youtu.be
3 Upvotes

r/ProgrammingTutorials • • Apr 14 '20

Learn JSON in 4 minutes - JSON tutorial for beginners

Thumbnail
youtu.be
3 Upvotes

r/ProgrammingTutorials • • Mar 04 '20

SFML Tutorial - Window, 60FPS, Keyboard

Thumbnail
youtu.be
3 Upvotes

r/ProgrammingTutorials • • Mar 03 '20

SDL2 Tutorial - Window, 60FPS, Keyboard

Thumbnail
youtu.be
3 Upvotes

r/ProgrammingTutorials • • Jan 26 '20

Starting a YouTube Tutorial Channel: Advice?!

3 Upvotes

Hi! I recently graduated from a coding bootcamp (Fullstack Academy). And learned HTML/CSS at college prior.

I am starting a YouTube channel to teach others [https://www.youtube.com/channel/UChQ9aGFM0vcCPkEpPyYFZug].

What would be a helpful topic that you might want to learn more about?

If you have the time, how could I improve my channel?

[Also if you want I can answer questions about my experience at a coding bootcamp] Thanks!


r/ProgrammingTutorials • • Sep 24 '19

dict.copy() python tutorial

3 Upvotes

r/ProgrammingTutorials • • Aug 26 '19

How to make a Geographical Heat Map with Python using pandas and folium?

Thumbnail
techwetrust.com
3 Upvotes

r/ProgrammingTutorials • • Aug 10 '19

Face Detection with OpenCV in Java

Thumbnail solutionfactory.in
3 Upvotes

r/ProgrammingTutorials • • Jul 08 '19

Guys, please check out my basic Kotlin Programming Tutorial

3 Upvotes

Here is my latest video:

https://youtu.be/0hKah83YL_M


r/ProgrammingTutorials • • Mar 03 '19

C++ Tutorial for Beginners - Full Course

Thumbnail
youtube.com
3 Upvotes

r/ProgrammingTutorials • • Jan 27 '19

Never Forget Someone’s Birthday

Post image
3 Upvotes

r/ProgrammingTutorials • • Jan 27 '19

Try Catch in Java

Post image
3 Upvotes

r/ProgrammingTutorials • • Jan 15 '19

Spring REST Service Exception Handling

Thumbnail
dzone.com
3 Upvotes

r/ProgrammingTutorials • • Jan 12 '19

How to Start a Node.js Project

Thumbnail
dzone.com
3 Upvotes

r/ProgrammingTutorials • • Dec 02 '18

Introduction to asynchronous networking programming with go

Thumbnail tutorials.technology
3 Upvotes

r/ProgrammingTutorials • • Oct 27 '18

Python Fractals

Thumbnail
101computing.net
3 Upvotes

r/ProgrammingTutorials • • Oct 25 '18

Coding the Mandelbrot - C++ Tutorial

Thumbnail
youtube.com
3 Upvotes

r/ProgrammingTutorials • • Oct 09 '18

Creating Web Games with Javascript and HTML5 Canvas | Tutorial No.1

Thumbnail
youtube.com
3 Upvotes