Skip to content

Latest commit

 

History

History
92 lines (55 loc) · 5.23 KB

1st_year_PreliminaryTask_CP.md

File metadata and controls

92 lines (55 loc) · 5.23 KB

Competitive Programming Authors - Adithiyan PV and Kaushik Kumbhat

TASK:

CREATE AN ACCOUNT IN HACKERRANK PLATFORM AND JOIN IN THE HACKERRANK CHALLENGE VIA THE LINK GIVEN BELOW:

LINK: https://www.hackerrank.com/prelim-task-1st-year

SCORING CRITERIA:

  • Each challenge has a pre-determined score.

  • A participant’s score depends on the number of test cases a participant’s code submission successfully passes.

  • If a participant submits more than one solution per challenge, then the participant’s score will reflect the highest score achieved. In a game challenge, the participant's score will reflect the last code submission.

  • Participants are ranked by score. If two or more participants achieve the same score, then the tie is broken by the total time taken to submit the last solution resulting in a higher score.

Study:

  1. Basic input and output:

In programming, input and output are fundamental concepts that allow a program to interact with its environment, typically by receiving data from the user (input) and presenting results or information (output).

A simple example of python code involving both user input and output:

# Simple calculator

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

operation = input("Enter an operation (+, -, *, /): ")

if operation == "+":
    result = num1 + num2
elif operation == "-":
    result = num1 - num2
elif operation == "*":
    result = num1 * num2
elif operation == "/":
    result = num1 / num2
else:
    result = "Invalid operation"

print(f"The result is: {result}")
  1. Conditional statements:

Conditional statements are a fundamental concept in programming that allows you to make decisions based on certain conditions. These statements enable your code to execute different blocks of code depending on whether specific conditions are met or not. In this blog post, we'll delve into the basics of conditional statements, starting with the ubiquitous if-else statement and gradually exploring more complex scenarios.

A simple flowchart demonstrating the working of conditional statement is shown as below:

image

  • 'if statement' is used to execute a block of code only if a certain condition is met. It allows us to conditionally execute code based on whether the specified condition is true.
  • 'else statement', on the other hand, is an optional companion to the if statement. It specifies what code to execute if the condition in the if statement is not met (i.e. if it is false).
  1. Introduction to 'for' loop:

A for loop is a control structure in programming that allows you to execute a specific block of code repeatedly. It's especially useful when you want to perform the same task multiple times without duplicating your code. Let's break down the essential components of a for loop:

Initialization: You declare and initialize a variable that serves as a counter. This step only happens once at the beginning.

Condition: You specify a condition that determines when the loop should stop executing.Increment/Decrement: You define how the counter variable changes after each iteration.

image

image

  1. Introduction to 'while' loop:

A while loop follows a simple sequence of steps:1. Evaluation of Test Expression: The loop begins by evaluating a test expression.Condition Check: If the test expression is true, the code inside the loop's body is executed.

Re-evaluation: After executing the code, the test expression is evaluated again.Continuation or Termination: This process continues until the test expression becomes false, at which point the while loop terminates.

image

While loops can be best illustrated with the practical example of finding the Factorial. Factorial of a number 'n' is the product of all positive integers from 1 to 'n'. To compute this using a while loop, we initialise a factorial variable to 1 and repeatedly multiply it by 'n' while decrementing 'n' until 'n' becomes 0. This ensures that we calculate the factorial correctly.

5. Introduction to functions:

In programming, a function is a block of code designed to perform a specific task. Functions help organize code, make it reusable, and improve its readability. Instead of writing the same code multiple times, you can define a function once and call it whenever you need it.

Why to use Functions?

  • Reusability: Write a function once, use it many times.
  • Organization: Break down complex problems into smaller, manageable parts.
  • Modularity: Functions allow you to organize code into separate sections.
  • Maintainability: Easier to update code. Changes in a function are reflected wherever the function is used.