# 05/07/21 Евчук Даяна
Во время пары:
## 1. Задача 1 [Last digit of a huge number](https://www.codewars.com/kata/5518a860a73e708c0a000027/train/cpp) **3kyu**
For a given list [x1, x2, x3, ..., xn] compute the last (decimal) digit of x1 ^ (x2 ^ (x3 ^ (... ^ xn))).
E. g.,
last_digit({3, 4, 2}) == 1
because 3 ^ (4 ^ 2) = 3 ^ 16 = 43046721.
Beware: powers grow incredibly fast. For example, 9 ^ (9 ^ 9) has more than 369 millions of digits. lastDigit has to deal with such numbers efficiently.
Corner cases: we assume that 0 ^ 0 = 1 and that lastDigit of an empty list equals to 1.
This kata generalizes Last digit of a large number; you may find useful to solve it beforehand.
*(Перевод)*
Для данного списка [x1, x2, x3, ..., xn] вычислить последнюю (десятичную) цифру x1 ^ (x2 ^ (x3 ^ (... ^ xn))).
Например,
last_digit ({3, 4, 2}) == 1
потому что 3 ^ (4 ^ 2) = 3 ^ 16 = 43046721.
Осторожно: силы растут невероятно быстро. Например, 9 ^ (9 ^ 9) содержит более 369 миллионов цифр. lastDigit должен эффективно работать с такими числами.
Угловые случаи: мы предполагаем, что 0 ^ 0 = 1 и последняя цифра пустого списка равна 1.
Это ката обобщает последнюю цифру большого числа; вы можете найти полезным решить эту проблему заранее.
### Код на JS
```
function trueMod(a, n, m) {
return Math.round( (a % m) * Math.pow( (a % m), (n + 3) % 4 ) ) % m;
}
function lastDigit(as){
if (as.length == 0) return 1;
let rightIsZero = false;
let rightBiggerThan2 = false;
let rightMod4 = 1;
for (let i = as.length - 1; i > 0; --i) {
console.log('With ' + i, as[i], rightIsZero, rightMod4);
if (rightIsZero) {
rightMod4 = 1;
rightIsZero = false;
rightBiggerThan2 = false;
} else {
rightMod4 =(rightBiggerThan2 && (as[i] % 4 === 2)) ? 0 : trueMod(as[i], rightMod4, 4);
rightIsZero = as[i] === 0;
rightBiggerThan2 = !rightIsZero && !(as[i] === 1)
}
}
console.log(rightIsZero, rightMod4)
return rightIsZero ? 1 : trueMod(as[0], rightMod4, 10);
}
```
#### Написанный на паре вместе с преподавателем код на C++
```
#include <iostream>
using namespace std;
/* Iterative Function to calculate (x^y)%p in O(log y) */
int power(long long int x, long long int y, long long int p)
{
long long int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// C++ function to calculate
// number of digits in x
int numberOfDigits(int x)
{
int i = 0;
while (x) {
x /= 10;
i++;
}
return i;
}
// C++ function to print last k digits of a^b
void printLastKDigits(int a, int b, int k)
{
cout <<" " ;
// Generating 10^k
int temp = 1;
for (int i = 1; i <= k; i++)
temp *= 10;
// Calling modular exponentiation
temp = power(a, b, temp);
// Printing leftmost zeros. Since (a^b)%k
// can have digits less then k. In that
// case we need to print zeros
for (int i = 0; i < k - numberOfDigits(temp); i++)
cout << 0;
// If temp is not zero then print temp
// If temp is zero then already printed
if (temp)
cout << temp;
}
// Driver program to test above functions
int main()
{
int a ;
int b ;
int k = 1;
cin>>a>>b;
printLastKDigits(a, b, k);
return 0;
}
```
## 2. Доделала задание 1.2 на [Replit](https://replit.com/@DayanaEvchuk/Arrba#script.js)
## 3. Закончила [вторую задачу](https://www.codewars.com/kata/582cb0224e56e068d800003c/train/cpp) **8kyu**
Nathan loves cycling.
Because Nathan knows it is important to stay hydrated, he drinks 0.5 litres of water per hour of cycling.
You get given the time in hours and you need to return the number of litres Nathan will drink, rounded to the smallest value.
For example:
```
time = 3 ---> litres = 1
time = 6.7 ---> litres = 3
time = 11.8 ---> litres = 5
```
*(Перевод)*
Натан любит ездить на велосипеде.
Поскольку Натан знает, как важно избегать обезвоживания, он выпивает 0,5 литра воды за час езды на велосипеде.
Вам дается время в часах, и вам нужно вернуть количество литров, которое Натан выпьет, округленное до наименьшего значения.
Например:
```
время = 3 ---> литров = 1
время = 6,7 ---> литров = 3
время = 11,8 ---> литров = 5
```
### **Решение**
### C++
```
#include <iostream>
using namespace std;
int litres(double t) {
return (t /2);
}
```
### JS
```
function litres(time) {
return Math.floor(time/2);
}
```
## 4. Приступила к задаче 3 [Build a square](https://www.codewars.com/kata/59a96d71dbe3b06c0200009c) и выполнила её
I will give you an integer. Give me back a shape that is as long and wide as the integer. The integer will be a whole number between 1 and 50.
Example
n = 3, so I expect a 3x3 square back just like below as a string:
```
+++
+++
+++
```
*(Перевод)*
Я дам вам целое число. Верните мне фигуру такой же длины и ширины, как целое число. Целое число будет задано числом от 1 до 50.
Пример
n = 3, поэтому я ожидаю возврата квадрата 3x3, как показано ниже, в виде строки:
```
+++
+++
+++
```
### **Решение**
### C++
```
public class generateShape(int integer)
{
string str = "";
for (int i = 0; i<integer; i++)
{
for (int j = 0; j<integer; j++)
{
str+="+";
}
if (i!=integer-1){
str+="\n";
}
}
return str;
}
```
### JS
```
function generateShape(integer)
{
let str = "";
for (var i = 0; i<integer; i++)
{
for (var j = 0; j<integer; j++)
{
str+="+";
}
if (i!=integer-1){
str+="\n";
}
}
return str;
}
```
## 5. Приступила к задаче 4 [Kaprekar Split](https://www.codewars.com/kata/5b6ee22ac5cc71833f0010d7)