What are some useful or interesting infinite generators? [closed] - language-agnostic

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
What are some clever uses for infinite generators? I've seen lots of seemingly trivial examples like "list all even numbers", but I assume there must be others that have more applicability to real-world scenarios. Concrete examples (in any language that support generators) appreciated!
I'll give a trivial sample as an answer.

Look at the Haskell code on http://rosettacode.org/wiki/Hamming_numbers#Haskell; that uses lazy lists (which are somewhat like generators) in a creative way to list all Hamming numbers.

A random generator might be considered clever use.

Trivial example: yield Fibonacci numbers one at a time (sans overflow checking, in C#):
public static IEnumerable<double> Fibonacci()
{
double n_minus2 = 1;
double n_minus1 = 1;
yield return n_minus2;
yield return n_minus1;
while(true)
{
double n = n_minus2 + n_minus1;
yield return n;
n_minus2 = n_minus1;
n_minus1 = n;
}
}

Related

How can I create bulk Hyperlinks (Parts 1 - 100) [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 4 years ago.
Improve this question
I am currently creating a website and I have come across the problem of having over 100 links and I don't know how to Hyperlink every single one.
Lets say I wanted to structure it like
<a href="link.com/1-5JKFKS" target="_blank">Part 1</>
<a href="link.com/2-5KWMAS" target="_blank">Part 2</>
<a href="link.com/3-42JNSL" target="_blank">Part 3</>
How would one go about creating 106 links formatted like this. I'm not exactly sure what I am looking for or even what is required to do this. So I'm sorry for how broad the question is.
I created a program in C# to create it for me. If anyone ever needs it I can give out the source code.
https://i.imgur.com/7csfx3V.gifv
I don't quite understand what you're asking for, but this would work as far as I can tell:
var nOfLinks = 100;
for (var i = 1; i <= nOfLinks; i++) {
document.getElementById("links").innerHTML += "<a href=\"link.com/" + i + "\" target=\"_blank\">Part " + i + "</><br/>"
}
<div id="links">
</div>
If you can use Emmet in the code editor you're using you can do it with something like this:
a[href="link.com/$" target="_blank"]{Part $}*106
With Emmet you just write this and usually press the tab key to expand the code into the full HTML.
That will output the links correctly.

how to get first JSONs elements using 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 5 years ago.
Improve this question
The public API respond me with JSON which has the next format
{"ABCD-EFG":
{"param1":"0.1234",
"param2":"0.123456",
"param3":"0.12334254"},
"HIG-KLMN":
{"param1":"0.3456",
"param2":"0.05710"
"param3":"0.004903"
... }
How can i get the List of the names (in this examle ABCD-EFG, HIG-KLMN) using Python3 ?
They can make changes in it every API get request sending
If it was like 'name' : 'ABCD-EFG', it would be easy. But it's not like that.
There are a few ways, here's one:
x = {
"ABCD-EFG": {
"param1":"0.1234",
"param2":"0.123456",
"param3":"0.12334254"
},
"HIG-KLMN": {
"param1":"0.3456",
"param2":"0.05710",
"param3":"0.004903"
}
}
y = list(x.keys())
print(y)
for key in x.keys():
print(key)
gives:
['ABCD-EFG', 'HIG-KLMN']
ABCD-EFG
HIG-KLMN

Swift Industry standard for curly braces [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
In Swift, I am trying to figure out whether I should do
if(true)
{
//stuff
}
else
{
//other stuff
}
Or
if(true){
//stuff
} else{
//other stuff
}
I know that technically it doesn't make a difference, but I was wondering what the industry standard was, and why the standard is...the standard.
Bracket style is usually a matter of opinion.
However, in this case, there is something to go by. Apple uses the second syntax you have provided exclusively in all of its documentation, with one distinction for Swift: parentheses.
From The Swift Programming Language Guide – Control Flow:
In addition to for-in loops, Swift supports traditional C-style for loops with a condition and an incrementer...
Here’s the general form of this loop format:
for initialization; condition; increment {
statements
}
Semicolons separate the three parts of the loop’s definition, as in C.
However, unlike C, Swift doesn’t need parentheses around the entire
“initialization; condition; increment” block.
In other words, you don't need parentheses around your conditional statements (in any type of loop or logic statement), and this is typically how Apple uses it in the documentation.
So, in the sample you have provided, Apple would use this style (note the spacing between the curly braces as well):
if condition {
// Stuff
} else {
// Other stuff
}
Some other examples from the docs:
// While loops
while condition {
statements
}
// Do-while loops
do {
statements
} while condition
// Switch statements
switch some value to consider {
case value 1:
respond to value 1
case value 2,
value 3:
respond to value 2 or 3
default:
otherwise, do something else
}
I have worked for different company and each of them is using different standard/coding rules.
When it comes to Apple and looking at their Swift documentation, it looks like they are using your second option.

Give a recursive function r on A that reverses a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I really need help with this task here. Im stuck at it and I really would appreciate your help
Here is the task:
Give a recursive function r on A that reverses a string. For instance,
r(logikk) = kkigol and r(moro) = orom. (given that A the amount of
letters in the Norwegian alphabet which has 29 letters.). Define the
function in such a way that it is correctly regardless of what A are.
Also logikk means logic in norwegian, and moro means fun in norwegian in case you're wondering.
I've tried to google on how I can solve tasks like this, but I had no luck. I hope I can get the help I need here.
Thanks a lot.
Here's a hint:
Define a function that takes a string argument.
If that argument is an empty string, return it. (Hint: this is not the best base case.)
Otherwise, return the last character of the string concatenated onto … what?
What is recursion?
If you think about the two questions here, you will have the answer.
(This answer is an attempt to stick to the spirit of https://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions/10812#10812.)
Here is the algorithm. I hope it would help you.
public String r(String str){
if(str.length() <= 1){
return str;
} else {
String reverse += str.charAt(str.length()-1)
+ r(str.substring(0,str.length()-1));
return reverse;
}
}

How to insert <p> tag in textfield when user hits "Enter" or "Return"? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my app, a user can fill out certain information about him on his profile. For example I have an about section, where the user can write a paragraph about his life. Right now, if the user types anything and saves the input, it is shown as one paragraph. I want it so that if the user hits enter, it should add a <p> tag when it is displaying the content.
Update
Just look at this question for example. When I type a question and hit enter
this text appeared on a new line
this is what I want!
You dont have to handle enter key.
Lets assume you are getting textarea value as a string str.
when you display it, just wrap every line with <p>
//`str` is textarea value
var str = document.getElementById("myTextarea").value;
var lines = str.split(/\r?\n/);
// create html
var html="";
for(var i=0;i<lines.length;i++){
html+='<p>'+lines[i]+'</p>';
}
// use html here showing inside some div
var divEl = document.getElementById("myDiv");
divEL.innerHTML = html;
I don't know what you are exactly asking for, but you can intercept the Enter key on inputs by doing such (with jquery).
$(".yourinputid").keyup(function (e) {
if (e.keyCode == 13) {
// Manipulate the DOM
}
});
I'm a bit confused about the <p> tag part. What do you want exactly to do?
Use nl2br() on POST value server-side (if on PHP; equivalents in other languages should be avaliable) or String.replace() on the textarea value client-side.