Coder Social home page Coder Social logo

zacharyrperales / hackerrank_c_solutions Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 42 KB

HackerRank C Challenge Solutions

Home Page: https://www.hackerrank.com/domains/c

License: MIT License

C 100.00%
c challenge guide tutorial challenges guides hackerrank hackerrank-c hackerrank-c-solutions hackerrank-challenges

hackerrank_c_solutions's Introduction

License HitCount Languages Top Language Last Commit


HackerRank C Challenge Solutions

Index

Introduction

"Hello World!" in C: Easy

Objective

In this challenge, we will learn some basic concepts of C that will get you started with the language. You will need to use the same syntax to read input and write output in many C challenges. As you work through these problems, review the code stubs to learn about reading from stdin and writing to stdout.

Task

This challenge requires you to print Hello, World! on a single line, and then print the already provided input string to stdout. If you are not familiar with C, you may want to read about the printf() command.

Playing With Characters: Easy

Objective

This challenge will help you to learn how to take a character, a string and a sentence as input in C.

To take a single character ch as input, you can use scanf("%c", &ch ); and printf("%c", ch) writes a character specified by the argument char to stdout.

char ch;
scanf("%c", &ch);
printf("%c", ch);

This piece of code prints the character ch.

You can take a string as input in C using scanf(“%s”, s). But, it accepts string only until it finds the first space.

In order to take a line as input, you can use scanf("%[^\n]%*c", s); where s is defined as char s[MAX_LEN] where MAX_LEN is the maximum size of s. Here, [] is the scanset character. ^\n stands for taking input until a newline isn't encountered. Then, with this %*c, it reads the newline character and here, the used * indicates that this newline character is discarded.

Note: The statement: scanf("%[^\n]%*c", s); will not work because the last statement will read a newline character, \n, from the previous line. This can be handled in a variety of ways. One way is to use scanf("\n"); before the last statement.

Task

You have to print the character, ch, in the first line. Then print s in next line. In the last line print the sentence, sen.

Input Format

First, take a character, ch as input. Then take the string, s as input. Lastly, take the sentence sen as input.

Constraints

Strings for s and sen will have fewer than 100 characters, including the newline.

Output Format

Print three lines of output. The first line prints the character, ch. The second line prints the string, s. The third line prints the sentence, sen.

Structs and Enums

Boxes through a Tunnel: Easy

You are transporting some boxes through a tunnel, where each box is a parallelepiped, and is characterized by its length, width and height. The height of the tunnel 41 feet and the width can be assumed to be infinite. A box can be carried through the tunnel only if its height is strictly less than the tunnel's height. Find the volume of each box that can be successfully transported to the other end of the tunnel. Note: Boxes cannot be rotated.

Input Format

The first line contains a single integer n, denoting the number of boxes. n lines follow with three integers on each separated by single spaces — ${length_i}$, ${width_i}$, and ${height_i}$ which are length, width and height in feet of the i-th box.

Constraints

  • $1 \leq n \leq 100$
  • $1 \leq length_i, width_i, height_i \leq 100$

Output Format

For every box from the input which has a height lesser than 41 feet, print its volume in a separate line.

Small Triangles, Large Triangles: Medium

You are given n triangles, specifically, their sides $a_i$, $b_i$, and $c_i$. Print them in the same style but sorted by their areas from the smallest one to the largest one. It is guaranteed that all the areas are different.

The best way to calculate a area of a triangle with sides a, b, and c is Heron's formula:

$S = \sqrt{p \times (p - a) \times (p - b) \times (p - c)}$ where $p = \frac{(a+b+c)}{2}$.

Input Format

The first line of each test file contains a single integer n. lines follow with three space-separated integers, $a_i$, $b_i$, and $c_i$.

Constraints

  • $1 \leq n \leq 100$
  • $1 \leq a_i, b_i, c_i \leq 70$
  • $a_i + b+i \gt c_i,a_i + c_i \gt b_i$ and $b_i + c_i \gt a_i$

Post Transition: Hard

We live in a big country. This country has towns towns_count in it. Each town has some post offices in which packages are stored and transferred.

Post offices have different inner structure. Specifically, each of them has some limitations on the packages it can store - their weight should be between min_weight and max_weight inclusively, where min_weight and max_weight are fixed for each office.

Packages are stored in some order in the office queue. That means, that they are processed using this order when sending and receiving.

Sometimes two post offices, even in different towns, may organize the following transaction: the first one sends all its packages to the second one. The second one accepts the packages that satisfy the weight condition for the second office and rejects all other ones. These rejected packages return to the first office back and are stored in the same order they were stored before they were sent. The accepted packages move to the tail of the second office's queue in the same order they were stored in the first office.

You should process several queries in your program. You'll be provided with structures package, post_office and town. In order to complete this task, you should fill the following functions:

print_all_packages given the town t, print all packages in this town. They should be printed as follows:

Town_name:
	0:
		id_0
		id_1
		...
	1:
		id_2
		id_3
		...
	...

where 0, 1 etc. are the numbers of post offices and $id_0$, $id_1$ ... are the ids of packages from the 0th post office in the order of its queue, $id_2$, $id_3$ are from the 1st one etc. There should be one '\t' symbol before post office numbers and two '\t' symbols before the ids.

send_all_acceptable_packages - given the towns source and target and post office indices source_office_index and target_office_index, manage the transaction described above between the post office #source_office_index in town source and the post office #target_office_index in town target.

town_with_most_packages - given all towns, find the one with the most number of packages in all post offices altogether. If there are several of them, find the first one from the collection towns.

find_towns - given all towns and a string name, find the town with the name name. It's guaranteed that the town exists.

Input Format

First line of the input contains a single integer towns_count. towns_count blocks follow, each describing a town.

Every town block contains several lines. On the first line there is a string town_name - the name of the town. On the second line there is an integer offices_count - the number of the offices in the town. offices_count blocks follow then, each describing an office.

Every office block also contains several lines. On the first line there are three integers separated by single spaces: packages_count (the number of packages in the office), min_weight and max_weight (described above). packages_count blocks follow, each describing a package.

Every package block contains exactly two lines. On the first line there is a string id which is an id of the package. On the second line there is an integer weight which is the weight of the package.

Then, there is a single integer queries on the line which is the number of queries. queries blocks follow, each describing a query.

Every query block contains several lines. On the first line there is an integer 1, 2, or 3. If this integer is 1, on the second line there is a string town_name - the name of town for which all packages should be printed. If this integer is 2, on the second line there are string source_name, integer source_office_index, string target_name and integer target_office_index separated by single spaces. That means transactions between post office #source_office_index in the town source_name and post office #target_office_index in the town `target_name should be processed.

If the integer is 3, no lines follow and the town with the most number of packages should be found.

Constraints

  • All integer are between 0 and 10.
  • towns_count $\gt 0$, offices_count $\gt 0$.
  • All strings have length $\leq 5$
  • All towns' names have only uppercase english letters and are unique.
  • All packages' ids have only lowercase english letters and are unique.
  • For each post office, min_weight $\leq$ max_weight.
  • All queries are valid, that means, towns with the given names always exist, post offices with the given indices always exist.

Output Format

For queries of type 1, print all packages in the format provided in the problem statement. For queries of type 3, print "Town with the most number of packages is town_name" on a separate line.

hackerrank_c_solutions's People

Contributors

zacharyrperales avatar

Stargazers

 avatar  avatar

Watchers

 avatar

hackerrank_c_solutions's Issues

Memory Can't be Traced After Reallocating and Moving Memory

Instead of a pointer to the first element of the array being stored for the source and target office package arrays as they initially contain, which would be in the heap(?), in the send_all_acceptable_packages function, those values become NULL in the debugger after reallocating and moving memory in the aforementioned function. I believe that this is due to the newly created moved memory being stored on the stack and therefore not being accessible to the debugger or something to that effect. This does not appear to affect program operations but it's not ideal.

This process needs to be clarified and/or resolved.

Check if Asserts and Unit Tests Can Assist in Solution

The memory management portion of this problem is difficult to fully comprehend and solve. Assert statements and unit tests may assist in narrowing the scope of the entire challenge to just the send_all_acceptable_packages function temporarily so that memory management solutions can be explored and more closely examined.

Refactor Unit Tests and Cover All Scenarios

The preliminary unit tests do not follow best coding practices.

  1. There is repeated code that needs to be refactored into reusable units.
  2. Only one scenario is covered for the send_all_acceptable_packages function. All logical scenarios should be covered for full coverage.

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.