iteration: add simple iteration function
This commit is contained in:
		
							
								
								
									
										12
									
								
								iteration/iter.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								iteration/iter.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | |||||||
|  | package iteration | ||||||
|  |  | ||||||
|  | const repeatTime = 5 | ||||||
|  |  | ||||||
|  | // Repeat takes a string and repeat it 5 times | ||||||
|  | func Repeat(s string) string { | ||||||
|  | 	var res string | ||||||
|  | 	for i := 0; i < repeatTime; i++ { | ||||||
|  | 		res += s | ||||||
|  | 	} | ||||||
|  | 	return res | ||||||
|  | } | ||||||
							
								
								
									
										12
									
								
								iteration/iter_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								iteration/iter_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | |||||||
|  | package iteration | ||||||
|  |  | ||||||
|  | import "testing" | ||||||
|  |  | ||||||
|  | func TestRepeat(t *testing.T) { | ||||||
|  | 	repeated := Repeat("a") | ||||||
|  | 	expected := "aaaaa" | ||||||
|  |  | ||||||
|  | 	if repeated != expected { | ||||||
|  | 		t.Errorf("expected %q but got %q", expected, repeated) | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user