How can we transform written number to number in C? [closed] - arrays

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
For example the input is three hundred and twenty six and the output is 326.
I have already tried to write all word of specific numbers like one,two,hundred,... in an array and the same for the integers . So they will have the same index like
word[0] is "one" and integer[0] is 1
But after that nothing works .

I really recommend starting with constructing a grammar. Here is an idea on how it can be done:
T :== t | td | d | x
H :== dh | dh&T
d - digit
x - ten, eleven or twelve
t - twenty - ninety
h - hundred
& - and
T - 1-99
H - 1-999
Note that this may be slightly incorrect because English is not my native language. But it shows how to construct a grammar.
So 326 could then be constructed by H -> dh&T -> dh&td
An expanded grammar to include thousands could look like this:
A :== Ha | HaH
a - thousand
312108 could be constructed with A -> HaH -> dh&Tadh&T -> dh&xadh&d

Related

Find the regular expressions of the following?Language of odd length and cannot contain length multiple of 3 over ššŗ={š’‚,š’ƒ} [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Language of odd length and cannot contain length multiple of 3 over
ššŗ={š’‚,š’ƒ}
OK to have an odd length that's not a multiple of 3, we can have a "head" part that generates strings of length 6n, and then a "tail" part that generates strings of length 1 and 5 (but not 7). Any odd number that's not a multiple of 3 can be written as 6n + 1 or 6n + 5 for n >= 0. So, let's get crackin.
r = ((a + b)^6)*((a + b) + (a + b)^5)
I have used a shorthand notation here which you can omit; basically, s ^ n stands for the regular expression s repeated n times. As long as n is a definite number this can be written out, just makes the regular expression a little longer.

Read input txt files and format it in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a input file that has different content and length each line.
Every line represents a command.
in
cr foo //create file "foo"
op foo //open it with index 1
wr 1 x 60 //write x in index 1("foo") 60 times
wr 1 y 10 //write y in index 1("foo") 60 times
sk 1 55 //such and such...
rd 1 10
dr
sv disk0.txt
in disk0.txt
op foo
rd 1 3
cr foo
cl 1
dr
I have looked at this answer, but it still not helpful for my question.
How to read specifically formatted data from a file?
I really need help of how to read this input file correctly in order to perform different commands.
The command may contain integers, or it main only have one character, I don't know how to deal with this text! Please help
Use fgets() to read till end of line.
Please make a point that fgets() comes with a new line char.
Break the line to tokens using strtok() with space as delimiter
Use the tokens accordingly.
Use strtol() to deal with integers

Troubles Finishing First Assignment - Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm doing my first programming course and I'm stuck already.
I'm trying to make a program that will tell you how many toy's you can make given how many pieces the user has.
To create the toy, you need to have 5 upper pieces and 2 lower pieces.
I've assigned the upper pieces to the letter 'a' and lower pieces to the letter 'b'
This is what I currently have done
print "Welcome to Amanda's Toy Factory"
print "At this factory, you will need to have 5 upper pieces and 2 lower pieces to create a toy"
x = input("How many toys would you like to make?")
print "To create",x,"toys, you will need", x*5, "upper pieces and", x*2, "lower pieces"
a = input("How many upper pieces did you bring?")
b = input("How many lower pieces did you bring?")
So for example, if you input you have 7 upper pieces and 5 lower pieces, it should tell you you are able to create 1 toy and that you would have 2 upper pieces left and 3 lower pieces left.
u, l = input('upper? '), input('lower? ')
nToys = min( int(u)/5, int(l)/2 )
upperLeft = u - nToys*5
lowerLeft = l - nToys*2
okey dokey. I take it you're using an older version of python because of the way you're using print. Here's how I'd do it:
a = input()
b= input("How many lower pieces did you bring?")
upper_left_over = a % 5
lower_left_over = b % 2
upper = int(int(a)/5)
lower = int(int(b)/2)
if upper > lower:
toys = lower
print("You can create", str(toys), "toys")
else:
toys = upper
print("You can create", str(toys), "toys")
print(upper_left_over, lower_left_over)
This is obviously messy. You can clean it up. Hope this helps!

Average distance between two randomly chosen indexes in array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Interesting thought question for you guys. Given an array of length n, if I were to pick two random indexes in this array, a and b on average how far apart would they be? As in how many steps would I have to take to walk from a to b. There are no restrictions so there's a chance I pick the same index for both, and there's a chance a and b are at opposite ends of the array.
I've thought about this for a while, my initial idea being they're on average n/2(ish) apart, but I think this hunch is incorrect. An index chosen in the center of the array at most would have to walk n/2 places to find its corresponding second choice, whereas only at the ends of the array would the second choice ever be around n distance away.
Thanks!
After scribbling some grids of possible distances for the first few values of n, I think the exact result is in fact given by:
f(n) = (nĀ² - 1) / 3n
Choosing two places in an array is equivalent to splitting the array up into 3 sections. The average size of each of those sections will be n/3 so the average distance between the two points is also n/3.
Using a monte carlo method in python:
from collections import defaultdict
import random
sample = [abs(random.choice(range(0,10)) - random.choice(range(0,10))) for i in range(0,10000)]
avg = float(sum(sample) / len(sample))
print ("Average: %f" % avg)
freq = defaultdict(int)
for s in sample:
freq[s] += 1
scale = 40.0 / max(freq.values())
for i in range(0,10):
print ("%d : %s" % (i, "#" * int(freq[i] * scale)))
Output:
Average: 3.293700
0 : ######################
1 : ########################################
2 : ####################################
3 : ###############################
4 : ##########################
5 : ######################
6 : #################
7 : #############
8 : #########
9 : ####
So, looks like it's n/3 - but it's not evenly distributed.
There is an easy way to know: for all the couples (a, b), computer their distance. Knowing that all the couples (a, b) have the same probability of appearance, you will just need to do the average of those distances in order to answer your question.

Find number of k-permutation with ordering but no repetition [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Im truggling to find a closed form for the number of k-permutations of a set S of cardinality n.
The combinations should regard ordering, but no repitions.
Example:
|S| = n = 3
S = {a,b,c}
k = 2
{a,b}
{b,a}
{b,c}
{c,b}
{a,c}
{c,a}
Anyone could help me out how to compute the number of viable permutations (and not the permutations itself)?
What I've tried:
I've read through different material and found out, that including repitititions it is
O(n) = n^k
My initial though was, that I need to eliminiate the permutations like
{a,a}
{b,b}
{c,c}
But I struggle finding a closed form for the number of perceivable repititions.
You are looking for the number of k-permutations of a set S of cardinality n.
The formulae is well known : n!/(n-k)!
Pseudo-proof :
for the 1st element, you are able to choose among the n elements of S ;
for the 2nd, only among : n-1, because you don't want doublons ;
...
for the ith, only among : n-(i-1) ;
...
for the kth, only among : n-(k-1) ;
So, finally :
n * (n-1) * ... * (n-i) * ... * (n-k+1)
= n! / (n-k)!

Resources