Explore 1.5M+ audiobooks & ebooks free for days

From $11.99/month after trial. Cancel anytime.

Basic Exercises for Competitive Programming: Python
Basic Exercises for Competitive Programming: Python
Basic Exercises for Competitive Programming: Python
Ebook79 pages43 minutes

Basic Exercises for Competitive Programming: Python

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Do you want practice for competitive programming?

This book contain more of twenty exercises with its solution written in programming language Python. Also include desktop testing for more comprension.

LanguageEnglish
PublisherJan Pol
Release dateJan 15, 2021
ISBN9781005034474
Basic Exercises for Competitive Programming: Python

Related to Basic Exercises for Competitive Programming

Related ebooks

Programming For You

View More

Reviews for Basic Exercises for Competitive Programming

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Basic Exercises for Competitive Programming - Jan Pol

    The following book shows a compilation of more than 20 basic exercises for competitive programming, all of them are written in Python. In addition, desktop tests are added to observe the operation of each algorithm.

    Hoping you can take knowledge of the pages of this book, my best wishes.

    Jan Pol

    Exercise 1

    Write a program that reads a string S, and prints that line on the standard output in reverse, that is, flipped right-to-left.

    Input

    The only input has a string S.

    Output

    Print the string in reverse

    Example

    Input

    1 2 3 hello

    Output

    olleh 3 2 1

    Solution

    One way to easily solve this task is to use a loop to print the characters from the last position to the first. This avoids the need to save the reverse list, just print each character.

    The code for the solution written in Python is shown below.

    1 l = input()

    2 i = len(l)

    3 while i > 0:

    4 i = i - 1

    5 print(l[i], end='')

    6 print()

    Desktop Testing

    Doing a desktop testing, it is observed how in each iteration each character is added inversely.

    Exercise 2

    Given of input a positive integer n. If n is even, the algorithm divides it by two, and if n is odd, the algorithm multiplies it by three and adds one. The algorithm repeats this, until n is one. For example, the sequence for n=5 is as follows:

    5→16→8→4→21

    Your task is to simulate the execution of the algorithm for a given value of n.

    Input

    The only input line contains an positive integer n.

    Output

    Print a line that contains all values of n separated with spaces.

    Example

    Input

    5

    Output

    5 16 8 4 21

    Solution

    To solve this task, a cycle is used to verify that the number is not yet 1. Within the cycle it is checked if the

    Enjoying the preview?
    Page 1 of 1
    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy