C Language: Majority Elections

The third week of Computer Science CS50 (Harvard) training begins and we continue to learn the C language. The first task: the implementation of a voting system where the candidate with the most votes wins, the so-called. plurality system or simply “winner takes all” (plurality.c). The user can choose one from the list of candidates and vote for him. When the votes are counted, the candidate with the most votes is declared the winner of the election.

Continue reading

Posted in C language (en) | Leave a comment

C language: Scrabble game with a friend

In the second week of the Computer Science CS50 (Harvard) course, after completing all the individual tasks, we move on to the C lab, which must be completed in a group. It will be an interesting task – to write the code for the game “Scrabble” (Scrabble.c). The essence of the task: the players come up with words that score a different number of points depending on the letters in this word, while each letter is assigned a different number of points from 1 to 10 and then these points are added up (the sum of the points of the letters gives the total number of points in the word).

For this task, the following table of correspondences of letters and points is used:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 3 3 2 1 4 2 4 1 8 5 1 3 1 1 3 10 1 1 1 1 4 4 8 4 10

Continue reading

Posted in C language (en) | Leave a comment

C Language: Advanced Caesarian Encryption

We continue to do homework from Problem set 2 in Computer science CS50 (Harvard). Last time, we learned how to encrypt text by shifting letters by a certain number (key) entered by the user. And this time we will solve the problem more complicated, it will be the Substitution.c program and it also encrypts the text by replacing some letters with others according to the principle that the user enters on the command line when the program starts. This principle looks like a string of letters of the alphabet arranged in a reversed order, as in the example:

$ ./substitution JTREKYAVOGDXPSNCUIZLFBMWHQ
plaintext:  HELLO
ciphertext: VKXXN

Continue reading

Posted in C language (en) | Leave a comment

C Language: Calculating the Level of Difficulty of a Text Using the Coleman-Liau Index

We continue to take the Computer Science CS50 (Harvard) course, and this time we will do the task of calculating the level of complexity of the text according to the Colman-Liau index (readability.c) in the C programming language. According to the idea of the two authors of the method, we can estimate the complexity of the text and thus distribute books by school grades (from simple texts, where the index is less than 1, to academically complex ones with an index greater than 16). The calculation is based on the formula:

index = 0.0588 * L - 0.296 * S - 15.8 ,
where L is the average number of letters per 100 words in the text,
S is the average number of sentences per 100 words in the text.
Continue reading

Posted in C language (en) | Leave a comment

C Language: Caesar Text Encryption

We continue to do homework in the Computer science CS50 (Harvard) course, and this time we need to write the program code and encrypt a text message.

The essence of the ceasar.c task is that the program asks for the user’s text input, and then displays a line of text below in encrypted form with the user’s key. The key is specified when starting the program on the command line after naming the program itself argv[]. So in the example of running the finished program, the user enters the key = 13 and his text “Hello, world!”, and at the output he receives this text in the form of a cipher:

Continue reading

Posted in C language (en) | Leave a comment

C language: how many coins does the cashier give

While studying Greedy Algorithms at CS50 (Harvard), let’s solve the problem about coins (Cash.c). Let’s say you need to give change to a customer in a store and you want to minimize the number of coins you give out to each customer. If you are a cashier, then you have coins (25 cents), ten cents (10 cents), five cents (5 cents) and a penny (1 cent) in the cash register. How to decide which coins and how much to transfer to the buyer?

Imagine a “greedy” cashier who wants to get as big a chunk out of this problem as possible with every coin he takes out of the drawer. Suppose you owe the customer 42 cents, then the largest first coin you can take is 25 cents. This choice will reduce the amount to be given away to 17 cents, since 42 – 25 = 17. It turns out that with the help of smaller coins we will need to solve a smaller problem. This approach from largest to smallest will give the smallest possible number of coins in change.

Solution (Igroglaz):

Continue reading

Posted in C language (en) | Leave a comment

C Language: Checking Credit Card Number Using Luhn’s Algorithm

In the cs50 course (Harvard) there is a C programming problem to determine the correctness of a credit card number through the Luhn algorithm (credit.c). Let’s analyze this problem and show two more solutions.

So. Any credit card has a number printed on it, which is also stored in the database of the bank that issued the card. Credit card numbers are generated in such a way that they have some kind of “checksum” – a mathematical relationship between at least one number and others. This checksum allows you to quickly perform initial checks and detect typos on the client side without having to make a request to the server.

This is where the Luhn Algorithm (created by Hans Peter Lun of IBM) comes in handy, which allows you to check the validity of the card in this way:

Continue reading

Posted in C language (en) | Leave a comment

C language: draw a pyramid from Mario

An interesting puzzle for beginners from CS50 (Harvard).

Task: draw a pyramid from the Mario (mario-less.c) game using # symbols (lattice or hash). The user is asked: “How many blocks to make a pyramid from?”, he enters a number and the pyramid is drawn, right-aligned, like this:

       #
      ##
     ###
    ####
   #####
  ######
 #######

igroglaz solution: Continue reading

Posted in C language (en) | Leave a comment

JavaScript – loop with do-while postcondition

In JavaScript, in addition to the while and for loops, there is another variant of the do-while loop:

do{
}while();

The meaning of this construct is that what is inside the curly braces is executed first, and then the while() condition is checked. If the condition is true, then the loop is repeated, if false, then the loop ends. Thus, the loop will be executed at least once. This loop is used less frequently than the previous two because it is easier for a human to understand a regular while loop to read the condition, rather than looking for the condition at the end of the loop. Continue reading

Posted in Javascript (en) | Leave a comment

JavaScript – while and for loops

JavaScript has two loops, while and for. They both run at the same speed. The for loop is more flexible – it has three parts separated by semicolons in parentheses. General entry:

for(first; second; third);

Inside the parentheses, the semicolon is the delimiter, not the end of the line.

first – any instruction that the machine will execute once at the very beginning of the loop, for example, you can initialize a variable.

Continue reading

Posted in Javascript (en) | Leave a comment

JavaScript – simple tasks for loops

Goal: write JavaScript code that calculates how much 2^10 is using a loop.

Solution:

varnum=2;
varpower=10;
varresult=1;
var subpower=1
while (subpower<=power){result*=num;subpower++};
document.write(result)

Continue reading

Posted in Javascript (en) | Leave a comment

JavaScript – how to embed code in html and display on the site

JavaScript code can be written in any notepad and saved with a .js extension – you can also embed the code in an html page using the <script>…</script> tags in both the head and body sections. It is better to insert the body into the body of the site, this will speed up the loading of the content of the site itself.

Thus, you can create a file on your computer with the html or htm extension, in which you can prepare the following code:

<html>
<script>

your JavaScript code

</script>
</html>

Continue reading

Posted in Javascript (en) | Leave a comment

JavaScript – Loops, Counter

A loop is a piece of code that repeats a certain number of times. The number of repetitions or the number of iterations is described with a counter.

The loop is written with while(an expression that is converted to a boolean type -> for the next iteration, you need to get true, as soon as we get false, the loop will stop);

An example of an infinite loop: while (true);

Which translates to false in JavaScript:

  • false
    undefined
    null
    0
    ” “
    NaN

Everything else is true.

Continue reading

Posted in Javascript (en) | Leave a comment

JavaScript – data output

Inside the html code, you can insert and execute JavaScript using the <script>…</script> tags, which can be inserted into the head of the page.

Data output is different depending on the interpreter:

var cat = “vera”; //cat’s name
document.write(‘Hi, ‘ + cat); print the result to the user
print(‘Hi, ‘ + cat); //one output in the Visual console, but in the browser this function starts printing the page on the printer
console.log(‘Hi, ‘ + cat); //another output option, works in the Google Chrome console
Hi, Vera //result

How to use external file in code?

To connect files in JavaScript, use the src (search) function inside the tags <script src=”file name”>…</script>

Posted in Javascript (en) | Leave a comment

JavaScript – comments

A comment is a piece of code that is not interpreted. There are multi-line comments and single-line comments.

Multiline comment starts with /* and ends with */

JavaScript code
/*
a comment
for a few
lines
*/
JavaScript code

A single line comment starts with // and goes to the end of the line

JavaScript code //comment for one line

Comments in the code are needed for developers who may want to fix or complete something in the code. It is important to leave comments along the code so that users and other developers can easily understand the code.

Posted in Javascript (en) | Leave a comment

JavaScript – good manners

In JavaScript, it is customary to name variables in small letters (if the name is one word) – cat; If the variable name consists of two words, then camel case notation is used, i.e. the first word is written in small letters, and the second is glued to the first and written with a capital letter: myCat; If there are several words, then the first one is written with a small letter, and each new word is glued with the previous ones and is written with a capital letter: myDearCat;

It is customary to declare variables using the var keyword, i.e. each new variable must be written on a separate line: var cat;

Continue reading

Posted in Javascript (en) | Leave a comment

JavaScript – Basic Operators

Comparison operator > / <

If we write 2 < 6 , we thus ask the question, is it true or not? The answer can be in the form of boolean values – True / False.

true > false

Equality operator (soft equals) ==

We ask, 6 == 6 (six equals six), the answer will be true

We cannot compare variables of different types, that is, we cannot compare a number and a string. If we write 6 == “6” then we are asking if the number six is equal to the string six. In this case, we will also get true, because the string will be converted to a number.

Continue reading

Posted in Javascript (en) | Leave a comment