Need ideas for an algorithm that generates an "echo text" from a given input - algorithm

After having some fun in a chatbox on a certain website I had an interesting idea. What would be an algorithm that, given some input text, would generate an "echo" text from it. That is, it generates the echo that you would hear if you shouted the input text in a large empty cave. For example:
Input text: Hello!
Output text: Hello!
ello!
lo!
o!
The problem is I don't exactly know what I want myself. I have no idea on how to create such an algorithm or what would even be a criteria to determine if it's a good algorithm. But I suppose the general idea is clear, so I'd like to hear your thoughts.
You don't have to give a complete solution in your answers. Hints of the direction too look in, or just random thoughts about the problem are welcome too.

Bad but fun (imo) solution:
Run the input through a speech synthesizer to generate a waveform.
Run that waveform through an echo generator in a sound processing library.
Then run the resulting waveform through a speech recognition program.

You could create an array of characters, and then interate over the list of characters, using a different starting point for each iteration.
For example in C#
string Mystring = "Is There an Echo in Here?";
char[] charArray = Mystring.ToCharArray();
int k = 0;
string Echo = "";
for (int i = 0; i < (charArray.Length / 3) + 1; i++)
{
for (int j = k; i < charArray.Length; i++)
{
Echo += charArray[j];
}
Echo += Environment.NewLine;
k += 3;
}
Should produce an output something like this
Is there an Echo in Here?
there an Echo in here?
re an Echo in here?
an Echo in Here?
Echo in Here?
o in Here?
n Here?
ere?
?
Just one possible way of doing things, and you can play around with the values to change the echo effect.
Another solution would be to split the string by the words rather than characters
string Mystring = "Is There an Echo in Here?";
string[] Words = Mystring.Split(' ');
int k = 0;
string Echo = "";
for (int i = 0; i < Words.Length / 2; i++)
{
for (int j = k; i < Mystring.Length; i++)
{
Echo += Mystring[j];
}
Echo += Environment.NewLine;
k += 2;
}
Would produce the following
Is there an Echo in Here?
an Echo in Here?
in Here?

Related

Conditional If not returning as expected

for (int i = 0; i < iMatrix1.Count(); i++)
{
if (Math.Abs(iMatrix1[i]) > 1E-20 & Math.Abs(iMatrix1[i]) < 0.0001) { oArrayList.Add(0); }
else { oArrayList.Add(iMatrix1[i]); }
}
Maybe it's just tired eyes... But I'm not getting my expected result, here. I'm attempting to eliminate erroneous numbers that are outside the lower bound for a given tolerance. (meaning, if they are outside of the range I'm trying to define, they need to be handled as zero)
For some reason, it's also changing numbers that are outside of this lower bound.
I'm sure this is just a silly mistake. Please set me straight.
My return value for iMatrix[i] = -0.000000000000000055
And clearly, 0.00000000000000000001 < |-0.000000000000000055| < .00001
Again, I'm sure that this is just a silly oversight... But it's killing me, at the moment.
Mhan7, if I get you correctly, you are trying to do this?
var a = 0.00000000000000000001;
var b = -0.000000000000000055;
var c = .00001;
Console.WriteLine($"a={c},b={b},c={c} | a < b = { a < Math.Abs(b)} | c > b = { c > Math.Abs(b)}");
if (Math.Abs(b) > a & Math.Abs(b) < c)
{
Console.WriteLine("Hello, World!");
}
Does that work on your computer - I used the same conditional as you. It appears that the above verifies that your code is correct, aka returns "Hello, World", because - as you said above:
a < Math.Abs(b) < c, or AKA 0.00000000000000000001 < |-0.000000000000000055| < .00001

Arduino 10x10 Matrix definition

EDIT
Solved
Yeah I know I'm stupid it hit me as I was showering :c
I have to use XY[9][9]... should not happen actually :|
So I'm 16 and I'm just trying out stuff... and I'm very sorry for my English.
I've built my own LED matrix(10 by 10) with an Arduino nano and now I want to program a Tetris like game for it.
So i thought it would be easier to use a 2D array then numbers from "0 to 99"
So sure i could use this:
int XY[10][10]
{
{0,1,2,3,4,5,6,7,8,9},
{10,11,12,13,14,15,16,17,18,19},
{20,21,22,23,24,25,26,27,28,29},
{30,31,12,33,34,35,36,37,38,39},
{40,41,42,43,44,45,46,47,48,49},
{50,51,52,53,54,55,56,57,58,59},
{60,61,62,63,64,65,66,67,68,69},
{70,71,72,73,74,75,76,77,78,79},
{80,81,82,83,84,85,86,87,88,89},
{90,91,92,93,94,95,96,97,98,99}
};
Well X and Y would be inverted but... yeah ^^ :D
But for learning sake i would like to do something like this:
int XY[10][10];
int c = 0; //Count
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
XY[i][j] = c;
c++;
}
}
So I'm doing something wrong
cause when I print out:
XY[10][10] I'm getting 0
and for c I get:
100
I know I'm just being stupid right now and it would be nice if someone could
help me
Thanks :)
Try using i<=9 and j<=9...worked for me.
Of course I have to tell the array that there are 10 elements (10by10) in the array but the first element is still "0" :| got confused there I'm sorry.

Is there any language that allows a break through multiple loops?

break interrupts a for-loop in most languages, but in the case of nested loops I have never encountered an n-th order break.
1. Is there such a thing in any language?
2. If so what is the correct name for it?
3. If not, why?
NB. I am not looking for workarounds.
Regarding point 3. The closest thing I know is goto, which should not be used as it leads to spaghetti code (Python has it only in a joke module), but this seems like a different problem as a boolean variable to mark an inner break, catching a raised a custom error or moving the block to a function in order to break with return are a lot more convoluted (in terms of line numbers and variables in the code).
(This is a curiosity question from a theoretical point of view, but if it helps, I code primarily in Python, Matlab and JS. I have know Perl, Pascal and Basic, but I know only the basics of C++ and know shamefully little of machine code.)
Java has a labeled break statement that lets you break out of any number of loops:
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search; // <<=== This statement breaks both nested loops
}
}
}
I don't know of any language that lets you do this (apart from #dasblinkenlight example - and not saying there aren't any) but you can easily emulate it in any language that has the break statement.
I.e. conditionnaly break on a boolean exit loop var.
var exitLoops = false;
for (var i = 0; i < x; i++) {
for (var j = 0; j < y; j++) {
for (var k = 0; k < z; k++) {
if (something) {
exitLoops = true;
break;
}
}
if (exitLoops) break;
}
if (exitLoops) break;
}
No there isn't (as far as i know). And why ? because if you need to exit several nested for loops all at once, then you have a code design problem, not a syntax problem. all the answers given above, except of #PinkTurtle , uses some sort of goto statement , which is not recommended .
In JavaScript you can do this
Copy paste the following the Chrome Dev Console,
free:
for(var i=0; i<10; i++) {
for(var j=0; j<10; j++) {
for(var k=0;k<10;k++){
console.log('I am at i='+i+' , j='+j+ ' , k='+k);
if(k==3) {
console.log('I will now break FREE...');
break free;
}
}
}
}
console.log('... And Now I am Here...')
Output
I am at i=0 , j=0 , k=0
I am at i=0 , j=0 , k=1
I am at i=0 , j=0 , k=2
I am at i=0 , j=0 , k=3
I will now break FREE...
... And Now I am Here...

How do I write an algorithm that allows for no-overflow natural number decrementing?

How can I write a function that takes a string denoting a natural number (>0) such as "100100000000" or "1234567890123456788912345678912345678901234567890" and returns a string denoting the input number decreased by 1? I cannot convert this string to an integer because it could overflow.
I am open to implementing this function in any popular language. I personally know c, C++, Java, javascript, python, and php.
k=len(x)-1
while(True):
if x[k]!='0':
x[k]-=1
break
else:
x[k]='9'
k--
I am leaving boundary conditions for you to work out.
Digit 1 is rather easy to decrease. Algorythm is simple:
Found any non-zero digit, if any
Copy digits before it, if any
Decrease found digit
Convert digits after it to 9
Remove 0 from begining of string
C# code
string res = "";
int nonZeroPos = -1;
int pos = s.Length - 1;
// Search for non-zero. TODO: check for digit
while((pos >= 0) && (nonZeroPos == -1))
{
if(s[pos] != '0')
{
nonZeroPos = pos;
}
pos--;
}
// TODO: if digit is NOT found
// Non changed part of number
for(int i = 0; i < nonZeroPos; i++)
{
res += s[i];
}
res += (char)(s[nonZeroPos] - 1);
for(int i = nonZeroPos + 1; i < s.Length; i++)
{
res += "9";
}
// TODO: kill 0 in the begining
If you want a near-unlimited capacity and want to write the algorithm yourself, process the string one digit at a time, from right to left, exactly as you would by hand.
In python overflow does not happen, python can hold any big number in practice, In C/C++ it is easy to write a string decrement similar to above algorithm by ElKamina. And Java has a BigInteger class

How can I convert a JavaScript for-loop to CoffeeScript?

for (i = 0; i < 10; i++) {
doStuff();
}
That's the JavaScript code that I Want to convert to CoffeeScript.
doStuff() for i in [0 .. 9]
This is explained on the introduction page: http://coffeescript.org/#loops
Edit/Update by JP:
The exact translation is:
doStuff() for i in [0...10]
You need to be careful with the ".." vs "...", for example:
count = 0
doStuff() for i in [0..count] #still executes once!
So you think, no problem... I'll just loop until count-1!
count = 0
doStuff() for i in [0..count-1] #executes twice!! '0' and then '-1'
Literal translation of:
for (var i = 0; i < someCount; ++i)
doStuff()
is
for i in [0...someCount]
doStuff()
The marked answer is functionaly correct but the generated code doesn't match the original javascript.
The right way (read, the one closest to the following javascript)
for (i = 0; i < 10; i++) {
doStuff();
}
is doStuff() for i in [0..someCount] by 1
Note the by 1 on the for loop.
Now this code, still creates an extra _i variable. If you can't live with it, then use the following:
i=0
while i<=someCount
doStuff()
i++
Previous answers work. However, dropping the i generates it better for me:
for [0...10]
doStuff()
or
doStuff() for [0...10]
The other solutions add an extra iterator variable i for you to use inside of the loop, for example doStuff(i), but from http://coffeescript.org/v1/#loops:
If you don’t need the current iteration value you may omit it:
browser.closeCurrentTab() for [0...count]
In detail, the translation of for i in [0...10] is for (i = j = 0; j < 10; i = ++j), whereas the translation of for [0...10] is for (i = 0; i < 10; i++).
Note the discussion in other comments about 2-dots versus 3-dots ([0..9] vs. [0...10]).

Resources