Python: Lists and Dictionaries

We continue to learn the Python programming language Python. We turn to the study of chapter 5 of the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where we will master lists, list methods and dictionaries.

Lists in Python

Lists are similar to tuples, which we studied in the last chapter. It’s important to remember that, unlike tuples, lists can change. The general principles for working with tuples apply to lists.
Continue reading →

Posted in Python (en) | Leave a comment

Python: Problems and Solutions (Chapter 4. For Loops, Strings, and Tuples. The Anagram Game).

We continue to practice programming. After the fourth chapter in the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I learned how to use the for statement and create tuples, tasks were suggested. Let’s do them together. I will give my solution, and you write your options in the comments.

1. Write a “Counting” program that would count at the request of the user. We should allow the user to enter the beginning and end of the count, as well as the interval between called integers. Continue reading →

Posted in Python (en) | Leave a comment

Python: for loops, constants, slices, tuples, sequences len, range, in, choice

We continue to learn the programming language. Let’s move on to chapter 4 of the book: Michael Dawson “Python Programming”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where we will study loops with the for () operator, introducing constants into code, slices and tuples, working with sequences on the example of strings with the operators len(), range(), in, jumble(), etc.

Summary of chapter 4 with examples of programs I wrote:

Cycles (loops) for

We already know loops with the while statement, and have even done a lot of tasks. Remember, there is a control variable introduced before the loop, which is then used in the condition, which in turn is tested for truth every round of the loop. So with the for loop, things are a little different. Continue reading →

Posted in Python (en) | Leave a comment

Python: Problems and Solutions (Chapter 3. Branching, while Loops, and Pseudocode. Guess the Number Game).

We continue to learn programming. After the third chapter in the book: Michael Dawson “We Program in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I studied the features of working with text in the Python programming language, tasks were proposed. Let’s do them together. I will give my solution, and you write your options in the comments.

Program “Guess the number” from Igroglaz without looking at the solution:

import random

guess = ""
guess_num = 0
number = int(random.randint(1,100))

print ("I made a guess: number 1-100. Can you guess it?\n")

while guess != number:
    guess = int(input("Enter your proposal: \n"))
    if guess > number:
        print("No, it's smaller..\n")
    elif guess < number:
        print("No, it's bigger..\n")
    else:
        print("Finally, you got it!\n")
    guess_num += 1

print ("Number was ", number, ". You guessed it with", \
guess_num, "times. Good job!")
    
input()

By the way, there is a bug in this program in the book. Outside the loop there is tries = 1; whereas it should start from zero.

1) Write a program – a simulator of a “surprise” pie – that would display one of five different “Surprises” at startup, chosen at random.

Continue reading →

Posted in Python (en) | Leave a comment

[C language ] Arithmetic progressions and MMORPGs

I am an avid MMORPG player and for me learning to program becomes much more fun and interesting if the programs I write have some connection to the games. This is especially important when it comes to some abstract mathematical things that are very difficult for me (humanities) to perceive.

Today we will look at such a thing as mathematical progressions. First, let’s write an arithmetic progression in C language, which will count how many monsters we need to kill to get a new level 😉 The program asks us to enter our initial experience (start), the amount of experience for killing one monster (step) and the amount of experience that is needed to get a new level (stop):

Continue reading →

Posted in C language (en) | Leave a comment

[C language] Operation precedence

It seems to me that I formatted the priorities of K&R operations more successfully:

→ ( )  [ ]  ->  .
← !  ~  ++   – –   +  –  *  &  (type)  sizeof
→ *  /  %
→ +  –
→ <<  >>
→ <  <=  >  >=
→ ==  !=
→ &
→ ^
→ |
→ &&
→ ||
← ?:
← =  +=  -=  *=  /=  %=  &=  ^=  |=  <<=  >>=
→ ,

 

Print version (picture 12Kb):

Continue reading →

Posted in C language (en) | Leave a comment

[C language] memcpy() function

I remember that when I saw the memcpy () function for the first time in the code, I did not understand what kind of unknown animal it was. No one even thought to write a comment that explains it (no, well, then you understand that it is logical .. not to write what printf does in the comments). At the same time, several hundred were scattered throughout the code of these memcpy. Right here are a handful of these functions, like clusters of ripe grapes hanging .. but how to eat them is not clear. Comments at best said something about copying, something about memory, buffer and other buzzwords.

So. memcpy() function – from English. memory copy – copies the contents of a memory cell from one area to another; the cells must not overlap. Defined with #include <string.h>

memcpy(where, where, size); Continue reading →

Posted in C language (en) | Leave a comment

[ C language] Symbolic I/O and Redirection (Chapter 8)

The eighth chapter in Steven Prata’s book “C Primer Plus” (in the new edition it is called “Character I/O and Input Validation”).

EOF (end of file) – designation of the end of the file. Ctrl+Z on Windows, Ctrl+D on UNIX.

Users do not really think about how word processors work, like notepad or Word. And they work thanks to the so-called. echoing the input, example:

Continue reading →

Posted in C language (en) | Leave a comment

[C language ] Insidious newlines on input

While doing the last exercise in Chapter 7, I ran into a problem reading an input line if it contains a newline. Everywhere this is explained somewhat abstrusely, I will try on my fingers:

The problem is that when you type any input into scanf from the keyboard – besides characters (letters, numbers, etc.) – you also _always_ send a /n character to the incoming stream – that is, a newline.

For example, the program contains the scanf operator ("%c", &n);

You press the q key on your keyboard and then the Enter key to send the value to the program…

Continue reading →

Posted in C language (en) | Leave a comment

[C language] Printf statement line break

I forgot how to properly “cut” long pieces of printf, so I decided to write this cheat sheet.

Let’s say you have a long printf expression and you need to break it into several smaller ones for readability (or to fit when embedding on a site, for example). There are three ways:

Continue reading →

Posted in C language (en) | Leave a comment

[C language ] Scanf character input (%d): infinite loop error

When completing tasks from the seventh chapter, for the first time I encountered a “pinched cycle”. Let’s say the user enters the character q into a field (don’t do this unless you have the ability to interrupt the program):

#include <stdio.h>
int main (void)
{
int n;
scanf("%d", &n);
while (n > 0)
{
    printf("Enter 0 to quit:\n");
    scanf("%d", &n);
}
getchar();
return 0;
}

The program will get stuck in an infinite loop when entering any character value. Why so?
Continue reading →

Posted in C language (en) | 1 Comment

[C language] Programming Exercises (Chapter 7)

Tasks from the seventh chapter of Stephen Prata’s book “C Primer Plus” – “C Control Operators: Branching and Jumps”.

1. Write a program that reads input until a # character is encountered, and then displays the number of spaces read, the number of newlines, and the number of all other characters.
Continue reading →

Posted in C language (en) | Leave a comment

[C language] C Control Statements: Branching and Jumping (Chapter 7)

The seventh chapter in Stephen Prata’s C Primer Plus. I want to start with a suitable divisors.c program

For a given integer, you need to print all the integers by which the given number is divisible without a remainder; if there are no such divisors, a message should be displayed stating that the number is prime.

A prime number (prime) has exactly two different divisors: one and itself;
If there are more divisors - this is a composite number (composite).

The first version of the program is looking for prime numbers in the forehead, by enumeration of divisors:
Continue reading →

Posted in C language (en) | Leave a comment

[C language] Programming Exercises: Loops and Arrays (Chapter 6)

The long-awaited moment has come to do “homework” on the sixth chapter of Stephen Prata’s book “C Primer Plus”. We are waiting for as many as 18 exciting tasks.

1. Write a program that creates an array of 26 elements and places them in
It has 26 lowercase letters of the English alphabet. Also consider displaying the contents of this array.

The very first program gave me a club on the head 🙂 It didn’t work for a long time, it always started with b, then it didn’t work .. The result, my first pancake:
Continue reading →

Posted in C language (en) | Leave a comment

[C language] Mysterious scanf and %c: enter a phrase, not a character

I want to share a separate post with a very non-trivial task from the point of view of a beginner in C. It is the eighth self-monitoring question in chapter 6 of Stephen Prat’s C Primer Plus.

The task is:

What will the following programs output when you type Go west, young man! ?
(In ASCII, the ! character follows the space character.)

#include <stdio.h>
int main(void) {
  char ch;
  scanf("%c", &ch);
  while (ch != 'g') {
    printf("%c", ch);
    scanf("%c", &ch);
  }
  return 0;
}

It would seem that everything is simple. It should output G – after all, %c in scanf is a request for a single character. More precisely, even so – the program is written incorrectly, tk. we are entering the whole phrase with %c, when it should be entered as a string %s. Anyway. This is a tutorial and all, so let’s assume… So the answer is G.

It wasn’t there. The program outputs:
Continue reading →

Posted in C language (en) | Leave a comment

[C language] Control Statements: Loops (Chapter 6)

Synopsis: The sixth chapter in Stephen Prata’s C Primer Plus.

Loop with precondition:
for (initialization; examination; update)
          operator

Initializing expression → test expression → corrective expression

Loop with postcondition:
do
    operator
while (expression)

The operational part is repeated until the expression becomes false (false), i.e. equal to zero.

Greek philosopher Zeno and the for loop

The Greek philosopher Zeno claimed that an arrow would never hit its target. At first, he said, the arrow flies halfway to the target. Then she flies half of the remaining path, then – half of the path that remains, and so on ad infinitum.
Continue reading →

Posted in C language (en) | Leave a comment

Compiling in “Notepad++”

I got a little carried away using Borland 6 to learn C. Powerful program and all, but due to my age, the UI customization capabilities are at the level of a notebook (the same one). For example, you cannot change the default colors for a code style. So I decided to move, at least in terms of the editor.

I’m a fan of Notepad++ 🙂 I really love this editor. And so I decided to tie the compiler to it. Notepad ++ is suitable for learning, although of course it is impossible to conduct large projects in it. Of the benefits – a convenient console that is always on the screen, you do not need to call it; in general, compiling in pros is quite simple, as it turned out: Continue reading →

Posted in C language (en), IDE (en), Notepad++ (en) | Leave a comment