Multi-stage selection (C # / Python) (old)

Multi-stage selection Answer date series:Yield practice/Nesting generators/Implementation of integer square root and cube root
problem http://nabetani.sakura.ne.jp/hena/ord24eliseq/
https://qiita.com/Nabetani/items/1c83005a854d2c6cbb69
Ruby 2014/8/2(On the day) https://qiita.com/cielavenir/items/9f15e29b73ecf98968a5
C#/Python 2014/8/4 https://qiita.com/cielavenir/items/a1156e6a4f71ddbe5dcb
Drop from here_prev_square/drop_prev_Answer before putting together cubic
Go/C#/Ruby/Python 2014/8/5 https://qiita.com/cielavenir/items/2a685d3080862f2c2c47
PHP/JavaScript 2014/9/9 https://qiita.com/cielavenir/items/28d613ac3823afbf8407
VB 2014/9/10 https://qiita.com/cielavenir/items/cb7266abd30eadd71c04
D 2015/12/21 https://qiita.com/cielavenir/items/47c9e50ee60bef2847ec
Perl 2017/3/10 https://qiita.com/cielavenir/items/6dfbff749d833c0fd423
Lua 2017/3/13 https://qiita.com/cielavenir/items/c60fe7e8da73487ba062
C++20(TS) 2017/3/15 https://qiita.com/cielavenir/items/e1129ca185008f49cbab (MSVC)
https://qiita.com/cielavenir/items/1cfa90d73d11bb7dc3d4 (clang)
F# 2017/3/17 https://qiita.com/cielavenir/items/a698d6a26824ff53de81
Boo/Nemerle 2017/5/13 https://qiita.com/cielavenir/items/e2a783f0fe4b0fe0ed48
Perl6 2017/5/15 https://qiita.com/cielavenir/items/656ea17fa96c865c4498
Kotlin 2017/5/25 https://qiita.com/cielavenir/items/9c46ce8d9d12e51de285
Crystal 2018/5/8 https://qiita.com/cielavenir/items/1815bfa6a860fd1f90db
MoonScript 2018/6/16 https://qiita.com/cielavenir/items/8b03cce0386f4537b5ad
Julia/Rust 2018/12/20 https://qiita.com/cielavenir/items/3ddf72b06d625da0c4a5
Nim 2018/12/26 https://qiita.com/cielavenir/items/5728944867e609fd52a7
Tcl 2018/12/31 https://qiita.com/cielavenir/items/76cbd9c2022b48c9a2c9
Pascal/Cobra 2019/1/16 https://qiita.com/cielavenir/items/81b81baf8dfc1f877903
Icon 2019/1/17 https://qiita.com/cielavenir/items/889622dcc721f5a4da24
Swift 2020/5/31 https://qiita.com/cielavenir/items/3b0b84a218e35d538f7f
Java/Groovy/Scala 2020/5/31 https://qiita.com/cielavenir/items/7f058203a8fd03b65870
(Regarding the implementation of icbrt)Lemma 2017/5/11 N even for integer division/(x*y)Is n/x/Equal to y(Proof of that)
https://qiita.com/cielavenir/items/21a6711afd6be8c18c55

Both C # / Python support infinite list.

C# ##

During the recital, I said, "I might be able to use IEnumerable," so it's the law of saying.

Since cbrt is DllImport, it depends on the environment. How to make an iterator was done by Mr. Masui's calculation of the shortest path (http://qiita.com/cielavenir/items/ac96da5e3040c2edb78c), but it is still troublesome.

By the way, what you can do with C # is possible even after VB2012, but I stopped because there is not much effort. It may be possible to make it by using the shortest path calculation source listed above. And only.

The rest is Java. But I don't know Stream. </ del> Stream.generate is not a coroutine, so it seems to be useless. I don't think there is anything that corresponds to Teka Enumerator.

hena24.cs


using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;

class Hena24{
	[DllImport("c")]
	private extern static double cbrt(double d);

	static private IEnumerable<int> generate(){
		int i=1;
		for(;;){
			yield return i;
			i+=1;
		}
	}
	static private IEnumerable<int> drop_prev_square(IEnumerable<int> _prev){
		IEnumerator<int> prev=_prev.GetEnumerator();
		prev.MoveNext();
		int a=prev.Current;
		prev.MoveNext();
		int b=prev.Current;
		for(;;){
			int x=(int)Math.Sqrt(b);
			if(x*x!=b)yield return a;
			a=b;
			prev.MoveNext();
			b=prev.Current;
		}
	}
	static private IEnumerable<int> drop_next_square(IEnumerable<int> _prev){
		IEnumerator<int> prev=_prev.GetEnumerator();
		prev.MoveNext();
		int a=prev.Current;
		prev.MoveNext();
		int b=prev.Current;
		yield return a;
		for(;;){
			int x=(int)Math.Sqrt(a);
			if(x*x!=a)yield return b;
			a=b;
			prev.MoveNext();
			b=prev.Current;
		}
	}
	static private IEnumerable<int> drop_prev_cubic(IEnumerable<int> _prev){
		IEnumerator<int> prev=_prev.GetEnumerator();
		prev.MoveNext();
		int a=prev.Current;
		prev.MoveNext();
		int b=prev.Current;
		for(;;){
			int x=(int)cbrt(b);
			if(x*x*x!=b)yield return a;
			a=b;
			prev.MoveNext();
			b=prev.Current;
		}
	}
	static private IEnumerable<int> drop_next_cubic(IEnumerable<int> _prev){
		IEnumerator<int> prev=_prev.GetEnumerator();
		prev.MoveNext();
		int a=prev.Current;
		prev.MoveNext();
		int b=prev.Current;
		yield return a;
		for(;;){
			int x=(int)cbrt(a);
			if(x*x*x!=a)yield return b;
			a=b;
			prev.MoveNext();
			b=prev.Current;
		}
	}
	static private IEnumerable<int> drop_num(int n,IEnumerable<int> _prev){
		IEnumerator<int> prev=_prev.GetEnumerator();
		int i=0;
		for(;;){
			i++;
			prev.MoveNext();
			int a=prev.Current;
			if(i%n!=0)yield return a;
		}
	}
	static private IEnumerable<int> drop_cnt(int n,IEnumerable<int> _prev){
		IEnumerator<int> prev=_prev.GetEnumerator();
		int i=0;
		for(;;){
			i++;
			prev.MoveNext();
			int a=prev.Current;
			if(i>n)yield return a;
		}
	}
	static void Main(){
		var f=new Dictionary<char,Func<IEnumerable<int>,IEnumerable<int>>>(){
			{'S',drop_next_square},
			{'s',drop_prev_square},
			{'C',drop_next_cubic},
			{'c',drop_prev_cubic},
			{'h',e => drop_cnt(100,e)},
		};
		for(int i=2;i<10;i++){
			int j=i; //Create a short-lived scope to avoid bugs in lambda-style captures.
			f[(char)('0'+j)] = e=>drop_num(j,e);
		}
		string line;
		for(;(line=Console.ReadLine())!=null;){
			bool first=true;
			foreach(int n in line.Aggregate(generate(),(s,e)=>f[e](s)).Take(10)){
				if(!first)Console.Write(',');
				first=false;
				Console.Write(n);
			}
			Console.WriteLine();
		}
	}
}

Python

It's just a bonus. Basically, the same logic as the Ruby version. cbrt uses scipy. I'm happy that Python becomes an external iterator without doing anything.

hena24_enum.py


#!/usr/bin/env python
#http://qiita.com/Nabetani/items/1c83005a854d2c6cbb69
#http://nabetani.sakura.ne.jp/hena/ord24eliseq/
import sys
import math
import itertools
from functools import partial,reduce
from scipy.special import cbrt # thx @ryosy383

'''
def generate():
	i=1
	while True:
		yield i
		i+=1
'''

def drop_prev_square(prev):
	a=next(prev)
	b=next(prev)
	while True:
		if int(math.sqrt(float(b)))**2!=b: yield a
		a,b=b,next(prev)

def drop_next_square(prev):
	a=next(prev)
	b=next(prev)
	yield a
	while True:
		if int(math.sqrt(float(a)))**2!=a: yield b
		a,b=b,next(prev)

def drop_prev_cubic(prev):
	a=next(prev)
	b=next(prev)
	while True:
		if int(cbrt(float(b)))**3!=b: yield a
		a,b=b,next(prev)

def drop_next_cubic(prev):
	a=next(prev)
	b=next(prev)
	yield a
	while True:
		if int(cbrt(float(a)))**3!=a: yield b
		a,b=b,next(prev)

def drop_num(n,prev):
	i=0
	while True:
		i+=1
		a=next(prev)
		if i%n!=0: yield a

def drop_cnt(n,prev):
	i=0
	while True:
		i+=1
		a=next(prev)
		if i>n: yield a

f={
	'S': drop_next_square,
	's': drop_prev_square,
	'C': drop_next_cubic,
	'c': drop_prev_cubic,
	'h': partial(drop_cnt,100),
}
for e in range(2,10): f[str(e)]=partial(drop_num,e)

if __name__=='__main__':
	try:
		while True:
			print(','.join(map(str,
				list(itertools.islice(
					reduce(lambda s,e:f[e](s),raw_input().rstrip(),itertools.count(1)),
				10))
			)))
	except EOFError:
		pass

Recommended Posts

Multi-stage selection (C # / Python) (old)
Multi-stage selection (Go / C # / Ruby / Python)
Multi-stage selection
python C ++ notes
python, openFrameworks (c ++)
C API in Python 3
ABC147 C --HonestOrUnkind2 [Python]
Extend python in C ++ (Boost.NumPy)
ABC163 C problem with python3
Python, Java, C ++ speed comparison
C / C ++ programmer challenges Python (class)
ABC memorandum [ABC163 C --managementr] (Python)
Python installation (Mac edition) (old)
I tried Python C extension
Python started by C programmers
ABC188 C problem with python3
ABC187 C problem with python
Solve ABC163 A ~ C with Python
Call C from Python with DragonFFI
ABC127 A, B, C Explanation (python)
ABC166 in Python A ~ C problem
Call popcount from Ruby / Python / C #
Introduction to Protobuf-c (C language ⇔ Python)
Solve ABC168 A ~ C with Python
ABC memorandum [ABC161 C --Replacing Integer] (Python)
Tips for calling Python from C
Execute Python code from C # GUI
How to wrap C in Python
ABC memorandum [ABC158 C --Tax Increase] (Python)
Solved AtCoder ABC 114 C-755 with Python3
C / C ++ programmer challenges Python (first step)
Solve ABC162 A ~ C with Python
Run Python scripts synchronously from C #
Solve ABC167 A ~ C with Python
ABC128 A, B, C commentary (python)
Solve ABC158 A ~ C with Python
ABC126 A, B, C Explanation (python)
Solve ABC037 A ~ C in Python
Write C unit tests in Python
AtCoder Beginner Contest 174 C Problem (Python)
Call C / C ++ from Python on Mac
Call c language from python (python.h)