Questions tagged [generator]

A generator is a generalization of a subroutine, primarily used to simplify the writing of iterators. The yield statement in a generator does not specify a co-routine to jump to, but rather passes a value back to a parent routine.

Filter by
Sorted by
Tagged with
2 votes
1 answer
79 views

Is using a generator of context managers kosher?

I know this is not necessarily a problem question but more of a style question. I understand that it doesn't have to be pretty for the most part. In general, context managers are used for mutexing, ...
georgwalker45's user avatar
4 votes
3 answers
146 views

Improving efficiency of Rust algorithm ported from Python generator

I'm learning Rust by solving ProjectEuler problems. To this end, I am trying to port a solution to problem 88 (link) in Python that heavily relies on generators to Rust (which doesn't have generators)....
Bram's user avatar
  • 93
2 votes
1 answer
172 views

Generator for colors

The goal here is to pull an item from an array and when the array ends - to start over - and allow you to pull items infinitely as needed. We don't use PHP that much, so please let us know how we can ...
sheriffderek's user avatar
6 votes
1 answer
2k views

Date range generator

An app has a feature that's much like any calendar application (like the Outlook calendar for example). Consequently, I need to do a lot of date logic. I wrote a class with date calculation methods ...
user2190492's user avatar
5 votes
0 answers
183 views

@lazylist - Converts a Python Generator to a List without evaluating every element

Inspired by a blog I read, I made a LazyList class that can wrap an iterable (list, generator etc.) and turn it into a ...
Greedo's user avatar
  • 2,345
2 votes
2 answers
70 views

Library to Help Loop with Generators

I found I need to loop through a list to create a brute force algorithm. Therefore, I decided to make a library, but to generalize the result by using a generator. There are three cases which is every ...
Akash Patel's user avatar
5 votes
1 answer
112 views

The python code simulates an environment of a disease and calculates how many people get infected

The python code simulates an environment of an X number of persons with an Y number of places they can go to, puts the persons randomly in the places and calculates how many persons get infected, die ...
R0Best's user avatar
  • 75
6 votes
4 answers
632 views

Bidirectional iterate over list in Python

I wrote a short function to iterate over a list starting at a given index, then flipping back and forth between left and right values. ...
Antyos's user avatar
  • 179
3 votes
2 answers
453 views

Q: Paying Debt Off a Year, MIT edX 6.001 U2/PS2/P1

Write a program to calculate the credit card balance after one year if a person only pays the minimum monthly payment required by the credit card company each month. The following variables contain ...
RacketeerHaskeller's user avatar
6 votes
2 answers
374 views

Docstring generator in Google type

Hello everyone! In this task I had to create docstring generator. I'm just wondering how you see this problem, I have to "take data" from the dictionary ...
KermitTheFrog's user avatar
1 vote
2 answers
397 views

Racing promises and consuming them in the order of their resolution time

The ideas are When multiple async calls are made, to be able to start consuming from the first resolving one regardless in what order the promises are fired. To construct a modern emitter of async ...
Redu's user avatar
  • 856
6 votes
1 answer
5k views

Compute the outputs of a generator in parallel

I am processing an unknown "length" of generator object. I have to keep things "lazy" because of memory management. The processing is compute heavy, so writing it multiproc style ...
Potter A's user avatar
2 votes
1 answer
59 views

Iterating through a list to generate calendars

I'm iterating through a list to generate calendars, the issue is each generator function has the same this.addToCalendar() function. I'd like to know how I could ...
Bonteq's user avatar
  • 153
1 vote
1 answer
109 views

Strong password generator with symbols that user definies

I've created program like in the title. Actually I know I could use regex to create pattern and then do some actions from it, but I think my code is clear enough (at least for me :P). There is a ...
El Patron Salan's user avatar
0 votes
2 answers
82 views

Python3: using generator to process a very large list of integers

This is from a leetcode question I am working on. The problem is the following: create an algorithm that works in the the following fashion: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]] the ...
lkilgoretrout's user avatar
3 votes
1 answer
68 views

Coroutine-based two-in-one properties

Over the holidays I played around with coroutines and their use as two-in-one getter and setter methods: ...
Richard Neumann's user avatar
8 votes
1 answer
2k views

A cryptarithm solver

Cryptarithms Cryptarithms, or Verbal arithmetic, are substitution puzzles where letters represent unique digits: S E N D + M O R E ========= M O N E Y ...
AJNeufeld's user avatar
  • 33.3k
3 votes
1 answer
128 views

Cyclic generator with access to current local state and executes callback after each cycle

I'm working on a Python class that is a cyclic generator, like itertools.cycle, but it offers access to its current state and executes a callback after the ...
Кафка's user avatar
1 vote
0 answers
71 views

Weekly Employee Report Generator PowerShell Script

Weekly Employee Report Generator PowerShell Script Description This script is used for generating a Markdown template file for weekly employee reports. In ...
Zoran Jankov's user avatar
9 votes
1 answer
868 views

Generate unique random strings considering collisions

I'm coding a random strings generator. It's working, but I'm not sure if this is efficient. ...
user2652379's user avatar
8 votes
4 answers
499 views

Lazy generator of strings in lexicographical order

I needed to write a Python generator that lazily generates, in lexicographical order, strings composed of a certain alphabet (e.g. lowercase English letters). My first thought was to make an infinite ...
Anakhand's user avatar
  • 645
3 votes
0 answers
96 views

Experimenting with C++20 coroutines to create Python-style generator functions

I've been playing around with the coroutines implementation in gcc 10.2.0 (Debian build) based on the sparse documentation at https://en.cppreference.com/w/cpp/language/coroutines . The target, for ...
Daniel Schepler's user avatar
4 votes
1 answer
212 views

Password Generator "Crapcode"

I made Password Generator on C#. It prompts the user for the length of the password, whether it contains numbers, lowercase letters, uppercase letters or symbols, and generates a strong password from ...
Tronzen's user avatar
  • 183
2 votes
2 answers
132 views

Feedback on generator functions and class composition in Python 3

I have written a Python script whose purpose is to read logs from CloudWatch and then post them to ElasticSearch. It is not quite finished but I've progressed far enough that I could benefit from ...
Alex Harvey's user avatar
2 votes
0 answers
226 views

Async linked list to and from async iterable

I'm working on some stuff that works better with (immutable async) linked lists such as this: ...
Juan's user avatar
  • 655
3 votes
1 answer
278 views

IP address exclusion algorithm

The below algorithm is supposed to take a list of input IPs/networks and a list of IPs/networks to exclude, and return a list of networks that don't include any of the IPs/networks to exclude... ...
deed02392's user avatar
  • 296
2 votes
0 answers
33 views

A word generator meant to be piped to hashcat/jrt which would then use amplificators (rules)

This piece of code does a linear generation with two 'tweaks': It sets a global loop as an index to reference variables that are holding values that need to be ...
Yvain's user avatar
  • 187
3 votes
1 answer
205 views

CSS preview generator (in python)

Given a CSS file the script generates an HTML preview of all the CSS definitions by recursively drilling down the chain of nested classes, ids and tag names. Rules with ...
ccpizza's user avatar
  • 145
6 votes
3 answers
1k views

Prime sieve generator

I wrote a simple prime sieve in Python. But it ended up looking... ugly. I included a few of the most basic optimizations. When posting this question SE suggested a bunch of questions on the same ...
martixy's user avatar
  • 253
4 votes
1 answer
2k views

Wrapping an asynchronous generator in Python

Since Python 3.6 and PEP 525 one can use asynchronous generator: ...
Delgan's user avatar
  • 551
1 vote
0 answers
78 views

Sudoku seed generator

The code returns a string ready to seed at sites like sudoku-solutions.com. It works great but as I couldn't find other simple examples for my level I hope for some insights on this and ways to ...
Yamil Duba's user avatar
1 vote
1 answer
56 views

Using generators on Observable class

I've decided to implement a simple case of the Observer pattern. I've gone about this considering you can only register 1 single observer in the Observable, instead of allowing multiple ones. I ...
BPL's user avatar
  • 157
1 vote
0 answers
48 views

infinite generator with eviction

I made a struct that will loop through a list, and return the next element on calls to the Get() method. It also allows to remove elements from the list by calling the Evict() method, and is safe for ...
aguilbau's user avatar
3 votes
1 answer
36 views

Creating generator object with image augmentation to train Convolutional Neural Networks with Keras

I am currently self-studying on Python generator object and use it to generate training data and do augmentation on-the-fly, then feed it into Convolutional Neural Networks. Could anyone please help ...
eng2019's user avatar
  • 131
1 vote
0 answers
143 views

Scraping pdf files with PyPDF2 and appending regex lists into Dataframe

I am looping through pdf files, using regex search and findall to append list type data into a dataframe. I export to excel for end user to easily search info. Below is method breakdown of this script:...
izzleee's user avatar
  • 111
3 votes
2 answers
646 views

Access a database and an API, uses python generators, and matches the results based on names, interview assignment

Thanks in advance if you are reading this code. I recently submitted this code as part of an interview (took about 4-5 hours). Unfortunately, they did not like the code and I received a form ...
Daniel R's user avatar
4 votes
1 answer
123 views

Check existence of user identification number

I was tasked with writing a python script to check if a passed ID number exists within a file. The file I was give contains roughly 25,000 unique ID numbers. An ID number is a unique 9 digit number. ...
Linny's user avatar
  • 9,989
3 votes
3 answers
99 views

ID generator class for my Repair Shop Software

I have made a ID generator class for my Repair Shop Software, that generates new ID for all data entities that are used in the software. It also converts int value ...
Zoran Jankov's user avatar
23 votes
8 answers
7k views

Random password generator in C

This is a simple password generator. What do you think about it? I am learning C for a while at school and at home. This just has a mix of symbols, lowercase, uppercase and numbers, with a ...
Karol's user avatar
  • 341
4 votes
1 answer
3k views

Creating an inverted index from text documents

I am working on an information retrieval project, where I have to process a ~1.5 GB text data and create a Dictionary (words, document frequency) and posting list (document id, term frequency). ...
Tasmeer's user avatar
  • 43
11 votes
3 answers
4k views

Generating sequential alphanumeric values that match a certain pattern

I'm working on generating large volumes of test data for some performance testing. Part of this is going to involve generating a lot of values that represent an "MBI", which is a certain kind of alpha-...
Dan Oberlam's user avatar
  • 7,764
1 vote
1 answer
67 views

A GapHelper static class to create unique (non-existing) size restricted names

I am working with a 3th party library which has following interface defined for a processing task: ...
Icepickle's user avatar
  • 825
7 votes
1 answer
302 views

Map unique raw words to a list of code words

Problem Write a function that replaces the words in raw with the words in code_words such that the first occurrence of each ...
nehcsivart's user avatar
2 votes
0 answers
29 views

Customizable unidirectional questionaire in javascript

I wrote the below answer for the following question on SO a couple of days ago, and I was curious if the usage of the generator function is an acceptable one. I choose the generator function, because ...
Icepickle's user avatar
  • 825
1 vote
1 answer
889 views

c++ python like range function to use in for loops

It's a simple range function that works like python range and it is only a syntactic sugar for the usual for loop if we want to operate on numbers in the range from 1 to 10 (1 ... 9) we use this code ...
dev65's user avatar
  • 303
2 votes
1 answer
88 views

Circular Doubly Linked List with generators

I didn't find an implementation on CR of the circular doubly linked list, using generators. Hence, I tried it myself. Description The class Deck represents a ...
dfhwze's user avatar
  • 13.9k
8 votes
2 answers
2k views

Python-style Generator in Java

I have implemented Python-style generators within Java. I might be reinventing the wheel here, but I feel that the ability to define a Generator with an anonymous class is the most flexible approach. ...
Avi's user avatar
  • 181
1 vote
1 answer
64 views

Random String Generation module in Python

The module is for facilitating random string generation. The example I use to explain it is the following: Instead of writing ...
cabralpinto's user avatar
1 vote
0 answers
95 views

An implementation of Project Wonderful's Infinite Auction

Project Wonderful is no more. ☹ As a little project, I've been trying to (slowly) re-implement the service. Here's my implementation of the Infinite Auction. The main function is ...
wizzwizz4's user avatar
  • 323
5 votes
1 answer
645 views

Generating combinations of specific values of a nested dict

I am developing a framework that allows to specify a machine learning model via a yaml file with different parameters nested so the configuration files are easy to ...
Jsevillamol's user avatar