How to make C program - c

How do I make a program in Python that takes an integer as input and prints it as a string?
Fore example, input : 77 -> output : seventy-seven

at first you should get input number length
for e.g (input->length) :
75 -> 2| 175 -> 3 | 9635 -> 4
then you should process it like this :
if length is 2 , first digit will be sth between twenty - ninety and the second will be sth between one - nine
if number just has 1 digit it will be sth between zero - nine
if number has 3 digit , 1st digit (from left) should be number + Thousand
for e.g : nine thousand and process to end
i hope i can explain what i want to say

Use an already available package i.e. https://pypi.org/project/num2words/
>>> from num2words import num2words
>>> num2words(42)
forty-two
>>> num2words(42, to='ordinal')
forty-second
>>> num2words(42, lang='fr')
quarante-deux
OR write your own code to do this using dictionary matching (would probably be a good start).

Related

Take two integers a,b and print all the even numbers in between them, excluding the input integers

Take two integers a,b and print all the even numbers in between them, excluding the input integers.
Constraints:
0 <= a < b <= 100
Input:
Two integers each in a new line.
Output:
Each line in the output contains an even integer between a,b in ascending order.
Example:
Input:
3
12
Output:
4
6
8
10
I could not find it's logic.
I don't get the question that well, but here's what I got out of it:
for i in range(a + 1, b):
if i % 2 == 0:
print(i)
This loop goes through every letter between your 2 constraints (a and b), checks if its divisible by 2, and if it is (which means it's even), it prints it.

How I can separate integer number in 3 "houses"? Hundred, Ten and Unity

I have, for example, a variable
int number = 300;
And I need modify "number" by "number", I wonder if I need separate in 3 variables for hundred, ten, unity or if there a method for divide that enable to me change a unique variable "number' by "number", "house" by "house", a hundred, a ten and unity (3 - 2 - 1).
Example: The user need only change number "2" of 3'2'1, and he want that "2" to being "5", as "321" must become to "351". In other words, the number 3 and 0 not will modified, only number 2 from 321, turning 3-5-1.
This has nothing to do with Arduino, it is C.
You can for example convert this to an array with itoa() (see https://playground.arduino.cc/Code/PrintingNumbers/)
And then convert it back to int with atoi() (see http://www.cplusplus.com/reference/cstdlib/atoi/)
Yes, you can do that, e.g. by using a function to change the digit.
Use the following steps:
What you do, is first shifting the digit right until it is the most right digit and remember the part removed
Clear the last digit.
Add the new digit
Shift left
Add the remembered part
Example (same steps as above) to change 321 to 351:
Shift right gives 32. Remember 1
Use the modulo operator and remove it: 32 - (32 % 10) = 32 - 2 = 30
30 + 5 = 35
35 -> after shift gives 350
350 + 1 = 351
I will leave the implementation up to you.

Taking x number integers and putting them into an array

I need to take x amount of input lines (x is specified by the user) then put them into a 2d array. Each line contains x amount of integers separated by spaces.
For example;
Input:
3
4 3 1
6 5 2
9 7 3
I need to take that input and put them into a 2d array, how do I do this?
Assuming your numbers are separated exactly by one space:
n = int(input('enter size'))
print([[int(i) for i in input().split(' ')]
for __ in range(n)])
Suppose you have your input stored in a file named 'input.txt'
n=2 #number specified by user
with open('input.txt', 'r') as file:
result = [[int(char) for char in lines.split(' ')]for lines in file.read().splitlines()[:2*n:2] ]

Integer compression method

How can I compress a row of integers into something shorter ?
Like:
Input: '1 2 4 5 3 5 2 3 1 2 3 4' -> Algorithm -> Output: 'X Y Z'
and can get it back the other way around? ('X Y Z' -> '1 2 4 5 3 5 2 3 1 2 3 4')
Note:Input will only contain numbers between 1-5 and the total string of number will be 10-16
Is there any way I can compress it to 3-5 numbers?
Here is one way. First, subtract one from each of your little numbers. For your example input that results in
0 1 3 4 2 4 1 2 0 1 2 3
Now treat that as the base-5 representation of an integer. (You can choose either most significant digit first or last.) Calculate the number in binary that means the same thing. Now you have a single integer that "compressed" your string of little numbers. Since you have shown no code of your own, I'll just stop here. You should be able to implement this easily.
Since you will have at most 16 little numbers, the maximum resulting value from that algorithm will be 5^16 which is 152,587,890,625. This fits into 38 bits. If you need to store smaller numbers than that, convert your resulting value into another, larger number base, such as 2^16 or 2^32. The former would result in 3 numbers, the latter in 2.
#SergGr points out in a comment that this method does not show the number of integers encoded. If that is not stored separately, that can be a problem, since the method does not distinguish between leading zeros and coded zeros. There are several ways to handle that, if you need the number of integers included in the compression. You could require the most significant digit to be 1 (first or last depends on where the most significant number is.) This increases the number of bits by one, so you now may need 39 bits.
Here is a toy example of variable length encoding. Assume we want to encode two strings: 1 2 3 and 1 2 3 0 0. How the results will be different? Let's consider two base-5 numbers 321 and 00321. They represent the same value but still let's convert them into base-2 preserving the padding.
1 + 2*5 + 3*5^2 = 86 dec = 1010110 bin
1 + 2*5 + 3*5^2 + 0*5^3 + 0*5^4 = 000001010110 bin
Those additional 0 in the second line mean that the biggest 5-digit base-5 number 44444 has a base-2 representation of 110000110100 so the binary representation of the number is padded to the same size.
Note that there is no need to pad the first line because the biggest 3-digit base-5 number 444 has a base-2 representation of 1111100 i.e. of the same length. For an initial string 3 2 1 some padding will be required in this case as well, so padding might be required even if the top digits are not 0.
Now lets add the most significant 1 to the binary representations and that will be our encoded values
1 2 3 => 11010110 binary = 214 dec
1 2 3 0 0 => 1000001010110 binary = 4182 dec
There are many ways to decode those values back. One of the simplest (but not the most efficient) is to first calculate the number of base-5 digits by calculating floor(log5(encoded)) and then remove the top bit and fill the digits one by one using mod 5 and divide by 5 operations.
Obviously such encoding of variable-length always adds exactly 1 bit of overhead.
Its call : polidatacompressor.js but license will be cost you, you have to ask author about prices LOL
https://github.com/polidatacompressor/polidatacompressor
Ncomp(65535) will output: 255, 255 and when you store this in database as bytes you got 2 char
another way is to use "Hexadecimal aka base16" in javascript (1231).toString(16) give you '4cf' in 60% situation it compress char by -1
Or use base10 to base64 https://github.com/base62/base62.js/
4131 --> 14D
413131 --> 1Jtp

C: how to store integers by reading line by line with a random layout?

I need to read a file and store each number (int) in a variable, when it sees \n or a "-" (the minus sign means that it should store the numbers from 1 to 5 (1-5)) it needs to store it into the next variable. How should I proceed?
I was thinking of using fgets() but I can't find a way to do what I want.
The input looks like this:
0
0
5 10
4
2 4
5-10 2 3 4 6 7-9
4 3
These are x y positions.
I'd use fscanf to read one int at a time, and when it's negative, it is obviously the second part of a range. Or is -4--2 a valid input?

Resources