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
1
vote
0answers
27 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 ...
0
votes
0answers
26 views

Connect-4 Python [closed]

I have the following Connect 4 implementation in Python. However for my check_won function I want to look for at least 4 matches within a given grid list, and stop ...
1
vote
1answer
25 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 ...
1
vote
0answers
32 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 ...
3
votes
1answer
26 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 ...
1
vote
0answers
47 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:...
3
votes
2answers
160 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 ...
4
votes
1answer
72 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. ...
3
votes
3answers
82 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 ...
23
votes
8answers
6k 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 ...
0
votes
0answers
21 views

Using function* (generator) to load external scripts in Angular application

I'm working on an Angular application that requires two external scripts to be loaded for certain components (about 12 separate ones). Script loading is only relevant if the user navigates to a ...
4
votes
1answer
295 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). ...
11
votes
3answers
826 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-...
1
vote
1answer
60 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: ...
7
votes
1answer
223 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 ...
2
votes
0answers
23 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 ...
2
votes
1answer
51 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 ...
7
votes
2answers
197 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. ...
1
vote
1answer
43 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 ...
1
vote
0answers
80 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 ...
4
votes
1answer
327 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 ...
5
votes
3answers
600 views

Python generator for “barely obtuse” triangles (Project Euler 224)

I am trying to solve Project Euler Problem 224: Let us call an integer sided triangle with sides a ≤ b ≤ c barely obtuse if the sides satisfy a2 + b2 = c2 - 1. How many barely obtuse triangles ...
4
votes
3answers
72 views

Take a limited number of items from a potentially infinite generator

Hypothetically, I have a bunch of generators that never end, like: function* progression() { let n = 0; while (true) { yield n++; } } I want to take ...
2
votes
0answers
192 views

One-handed Solitaire Game Choice Generator

I want to test some different endgame scenarios for one-handed Solitaire where the removal of the cards if the suit or value match is optional. I've written a simple generator which yields at the ...
1
vote
0answers
112 views

Merge K sorted Arrays (JavaScript using generators)

Question: Merge k sorted Arrays My main concerns about my code are: Performance: is it optimized and is my description of the time and complexity correct. How should I annotate the space and time ...
1
vote
0answers
80 views

Recursive Generator Exceeds Call Stack

I have this recursive generator that fetches 100 messages from an API and gets the next 100 until there are no more messages to get: ...
-1
votes
1answer
128 views

When to create a generator or return simply a list in Python? [closed]

I have this kind of instance method: ...
1
vote
2answers
6k views

Generating unique alphanumeric ID's of a fixed length

I am needing to generate unique id's for users. The 'unique' part is the problem for me. The 13 character id's which have a format of the following: 0 + random int with range of 1-9 + random letter ...
3
votes
1answer
400 views

Flatten nested iterables

I wrote a program sometime ago that reads csv files, sorts, reorders and groups their columns/rows' content (with itertools) and later labels the groups using a ...
0
votes
1answer
161 views

Python custom generator of object not efficient

Let's consider the following class Signal which defines a multiphasic signal: ...
2
votes
0answers
116 views

Generator coroutine class

Submitted for review ... A co-routine class called generator that co_yields values of a parameterized type. I am interested of course in whether it is bug-free. I ...
3
votes
1answer
455 views

Using generator for buffered read of large file in Python

I have a large file that I need to parse - and since it will be regenerated from external queries every time script runs so there is no way to parse it once and cache the results. I want to save ...
6
votes
1answer
3k views

C# recursive backtracker labyrinth generation & game

So for over a weekend I decided to write something rather simple. For maze generation I've followed this algorithm and aside from that I've made a little game, to go from A to B. Everything works fine ...
1
vote
2answers
199 views

Generator function to enumerate files

I want to implement a function that enumerates all files in directory. I think the function should Return a Promise, because underlying readdir call is prefered ...
1
vote
1answer
83 views

Transforming lists of products to boolean vectors

Task Inputs Lists of products stored in a CSV file, where each line represents the shopping list of a single client: Outputs A CSV file: Header: products names sorted by number of appearances in ...
0
votes
1answer
1k views

Generate a set of combinations

For one of my personal projects, I needed an algorithm to generate a set of combinations. I've finally successfully written one. The problem is that this algorithm uses two recursive calls to ...
1
vote
1answer
61 views

Modelling deadlock in cyclic pipelines in Haskell with coroutine libraries

I'm modelling two processes which have been put into a cyclic pipeline - the output of each feeds into the input of the other - so as to work out when they've deadlocked (i.e., neither can progress ...
5
votes
1answer
165 views

Cached infinite prime generator

For a project I am working on, I need to use a lot of primes, frequently. To do this, I added a cache to Will Ness's prime sieve, that stores already generated primes so getting them is quick. How can ...
7
votes
1answer
3k views

Use generator to do inorder traversal

I am not used to generators and wonder how to make code cleaner/Pythonic in the generator part. The function is about validating a binary search tree. The ...
2
votes
1answer
47 views

Python coroutine structure

I've completed Advent of Code's Day 9 in Python3, trying to use it to teach myself coroutines. I'm very familiar with generators, but I've pretty much never used coroutines before. I've ended up with ...
5
votes
2answers
211 views

Alternative to nonlocal for local variables in a function with nested functions in Python

Introduction for future readers I posted the code below in the hope that it would be a good example to discuss the topic "Alternative to nonlocal for local variables in a function with nested ...
7
votes
1answer
3k views

Tokenizing a file with a generator

I'm parsing (specifically tokenizing) a file, line-by-line. I've a method tokenize, that takes a string (one line of code, it can't take the whole file at once), breaks it into parts, and returns a ...
7
votes
3answers
571 views

Generator iterator with push back function

In a compiler project for an LL(1/2) grammar, I have had a need for a generator iterator with a push back function. I have been surprised not to find any perfectly applicable solution (clean, simple, ...
0
votes
1answer
98 views

Working with cached generator values

I have set of values represented by a class. The class should be initialised with a generator to limit memory use. The class itself is the iterable, having ...
2
votes
0answers
166 views

AsyncLock implementation for JS

I made an AsyncLock implementation due to a comment on Hacker News saying JavaScript was missing such a necessary idea. I honestly am not terribly familiar with Threaded programming and the concept of ...
5
votes
2answers
183 views

Asynchronous JavaScript to fetch remote JSON data with promises and generators

I am trying to figure out how to do async calls using promises and generators (without 3rd party libraries). Most of the examples I found on the Internet are either incomplete or confusing (at least ...
2
votes
2answers
176 views

Random Number Generator Followup: Choosing the Generator Algorithm and the Distribution

This question is a follow-up from my previous code review question. This question regards the ability to choose a predefined random number generator algorithm and also choose a generator distribution. ...
5
votes
2answers
400 views

Simple NBA Draft Order Generator

I wrote a simple program to create a random order for an NBA draft, prompt the user for input of draft picks, print the order of the draft and the results, and write the results to a text file. This ...
2
votes
1answer
63 views

My game of life implementation

I'm looking for comments on both the micro (individual lines of code) and macro (overall design) scale. My input/output format is a single string, with newlines delimiting rows and ...
4
votes
1answer
980 views

Implement a boto3 RDS generator in Python

Boto3 does not seem to implement a generator for RDS instances, instead offering a marker and pagination feature. This code does work for my use case and I have been using it for the entire day ...