New Deck of cards display in a textbox - playing-cards

private void GetNewDeck_Click(object sender, EventArgs e)
{
string[] suit = { "C", "D", "H", "S" };
string[] num = { "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A" };
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 13; i++)
{
NewDecktextBox.Text + = (suit[j] , num[i]"\n");
}
}
}
I was trying to display a new deck of cards in a multiline textbox(NewDecktextBox) when I click the button GetNewDeck_Click button.Iam having error with the NewDecktextbox.Text line..
The output should be
C2 C3 C4 C5 C6 C7 C8 C9 C10 CJ CQ CK CA
D2 D3 D4 D5 D6 D7 D8 D9 D10 DJ DQ DK DA
H2 H3 H4 H5 H6 H7 H8 H9 H10 HJ HQ HK HA
S2 S3 S4 S5 S6 S7 S8 S9 S10 SJ SQ SK SA
Thanks

This will be a bit better
StringBuilder sb = new StringBuilder();
foreach (String suit in new string[] { "C", "D", "H", "S" })
{
foreach (string value in new String[] { "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A" } )
{
sb.Append(suit);
sb.Append(value);
sb.Append(" ");
}
sb.Append(Environment.NewLine);
}
NewDecktextBox.Text = sb.ToString()
Strings are immutable in .net. You way would build a new string for every card you added to the textbox, so basically 52 of them "C2 " then "C2 C3 " then ....
As soon as you start manipulating a string in a loop, StringBuilder is the way to go.
Another tip is to use foreach if you can, your way if you wanted to add another card e.g. Emperor (invented by me just now), you'd have to change the loop variables and your arrays.
Happy learning.
Environment.NewLine is the line end for the environent you are in, automatically copes with LF or CRLF.

Related

Splitting a Scala List into parts using a given list of part sizes.[Partitioning]

I got two lists:
val list1:List[Int] = List(5, 2, 6)
val list2:List[Any] = List("a", "b", "c", "d", "e", "f", "g", "h", "i", "j","k")
such that list1.sum >= list2.size
I want a list of lists formed with elements in list2 consecutively
with the sizes mentioned in list1.
For example:
if list1 is List(5,2,4) the result I want is:
List(List("a", "b", "c", "d", "e"),List("f", "g"),List("h", "i", "j","k"))
if list1 is List(5,4,6) the result I want is:
List(List("a", "b", "c", "d", "e"),List("f", "g","h", "i"),List("j","k"))
How can I do that with concise code.
Turn list2 into an Iterator then map over list1.
val itr = list2.iterator
list1.map(itr.take(_).toList)
//res0: List[List[Any]] = List(List(a, b, c, d, e), List(f, g), List(h, i, j, k))
update:
While this appears to give the desired results, it has been pointed out elsewhere that reusing the iterator is actually unsafe and its behavior is not guaranteed.
With some modifications a safer version can be achieved.
val itr = list2.iterator
list1.map(List.fill(_)(if (itr.hasNext) Some(itr.next) else None).flatten)
-- or --
import util.Try
val itr = list2.iterator
list1.map(List.fill(_)(Try(itr.next).toOption).flatten)
you can slice on list2 based on the size you get from list1,
def main(args: Array[String]): Unit = {
val groupingList: List[Int] = List(5, 2, 4)
val data = List("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k")
//0, 5
//5, (5 + 2)
//5 + 2, (5 + 2 + 4)
val grouped = groupingList.zipWithIndex.map { case (_, index) =>
val start = groupingList.slice(0, index).sum
val end = groupingList.slice(0, index + 1).sum
data.slice(start, end)
}
println(grouped)
}
result: List(List(a, b, c, d, e), List(f, g), List(h, i, j, k))
Also read: How slice works
i like show it using ex
list1=List("one","two","three")
List[String] = List(one, two, three)
list2=List("red","green","blue")
List[String] = List(red, green, blue)
list1::list2
List[java.io.Serializable] = List(List(one, two, three), red, green, blue)
var listSum=list1::list2
List[java.io.Serializable] = List(List(one, two, three), red, green, blue)
listSum
List[java.io.Serializable] = List(List(one, two, three), red, green, blue)
we can "::" for insert one list to another list in Scala
Just found by me, even the following code worked, but the code posted by jwvh appears more concise than this, and I understand making list1 a Vector makes more sense regarding performance of element access in the following code:
list1.scan(0)(_+_).sliding(2).toList.map(x=>list2.drop(x(0)).take(x(1)-x(0)))
or
list1.scan(0)(_+_).zip(list1).map(x=>list2.drop(x._1).take(x._2))
list1.scan(0)(_+_).sliding(2).map(x=>list2.slice(x(0),x(1))).toList

get indexes of elements containig some value and the coresponding elements from another list

The queston is a mouthful, but the idea pretty simple.
I have 3 lists and a string.
val a = List("x", "y", "z")
val b = List("a1", "a2", "b1", "b2", "c1", "c2", "d1", "d2")
val c = List("1", "1", "2", "2", "3", "3", "4", "4")
val d = "xc1b1"
I need to check if d contains elements from a. If it does I check the position of all the elemtns from b that are present in d and return a set of elements from c that corespond these positions.
The result for the given example is
Set("3", "2")
But when I try
if(a.exists(d.contains)) c(b.indexWhere(d.contains))
I only get
Any = 2
Which corespond to the first encountered elemnt from b ie b1
How would I get the set?
-
if(a.exists(d.contains)) b.zip(c).collect{
case (x, y) if d.contains(x) => y
}
// res1: Any = List(2, 3)
If you need a Set:
if(a.exists(d.contains)) b.zip(c).collect{
case (x, y) if d.contains(x) => y
}.toSet
// res2: Any = Set(2, 3)
I think I've understood what you need to do here, although the question could do with some clarification.
These are the two ways of getting to your set that I found:
if(a.exists(d.contains)) b.collect {
case x if d.contains(x) => c(b.indexOf(x))
}.toSet
if(a.exists(d.contains)) b.filter(d.contains).map(b.indexOf).map(c).toSet
Both find elements of b that are in d, then find their index in b and find their relative elements in c. The first way is more explicit in what it's doing, while the second way is more concise.

Swift List Product

Given two lists in Swift:
let rows = ["a", "b", "c"]
let cols = ["1", "2", "3"]
Is it possible to combine them using list comprehension to produce the following:
squares = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]
Obviously, it can be done using a "for loop" and similar constructs, but I'm specifically for a list comprehension based solution. I know Swift can do some list comprehension (e.g. let evens = Array(filter(1..<10) { $0 % 2 == 0 }) ) but can't figure out if it can do something similar to the following piece of Haskell:
let squares = [ r ++ c | r <- rows, c <- cols]
A possible solution (now updated for Swift 2):
let rows = ["a", "b", "c"]
let cols = ["1", "2", "3"]
let squares = rows.flatMap {
row in
cols.map {
col in
row + col
}
}
print(squares)
// [a1, a2, a3, b1, b2, b3, c1, c2, c3]
The inner map() maps the cols array to an array with the
row number prepended to each entry. The outer flatMap() maps
the rows array to an array of arrays (one for each row) and flattens the result.
Slightly more general, one could define the "product" of two
sequences as an array of tuples (pairs) with all combinations:
func product<S : SequenceType, T : SequenceType>(lseq : S, _ rseq : T) -> [(S.Generator.Element, T.Generator.Element)] {
return lseq.flatMap { left in
rseq.map { right in
(left, right)
}
}
}
and then use it as
let squares = product(rows, cols).map(+)

Can I yield or map one element into many in Scala?

val inArray = Array("a", "b", "c", "d")
// ...
val outArray = Array("a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3", "d1", "d2", "d3")
How to map inArray to outArray?
The idea is to iterate through inArray yielding 3 elements (by concatenating an index in this example) from each of its elements.
You can do that with flatMap.
inArray.flatMap(c => (1 to 3).map(c+))
This can look better using a for-comprehension
for {
s <- inArray
i <- Array(1, 2, 3) //or other traversable
}
yield s + i
This uses a combination of map and flatMap under the covers as described in detail in the SLS
Using for-comprehension.
scala> for {
| x <- Array("a", "b", "c", "d")
| n <- 1 to 3
| } yield x + n
res0: Array[java.lang.String] = Array(a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3)

Proper way for looping in Scala

Suppose I have an array of strings in Scala:
val strings = Array[String]("1", "2", "3", "4", "5", "6", "7")
What I need is to make a new array which elements will be obtained as a concatenation of each three (any number) consequent elements of the first array, which should result in ("123", "456", "7")
Being new to Scala I wrote the following code which was neither concise nor effective:
var step = 3
val strings = Array[String]("1", "2", "3", "4", "5", "6", "7")
val newStrings = collection.mutable.ArrayBuffer.empty[String]
for (i <- 0 until strings.length by step) {
var elem = ""
for (k <- 0 until step if i + k < strings.length) {
elem += strings(i + k)
}
newStrings += elem
}
What would be the Scala way for doing this?
strings.grouped(3).map(_.mkString).toArray
or
strings grouped 3 map (_.mkString) toArray
I personally prefer the first version :)
strings grouped 3 map (_.mkString)
or (in order to really get an Array back)
(strings grouped 3 map (_.mkString)).toArray
... or use sliding
val strings = Array[String]("1", "2", "3", "4", "5", "6", "7")
strings.sliding (3, 3) .map (_.mkString).toArray
res19: Array[String] = Array(123, 456, 7)
Sliding: You take 3, and move forward 3. Variants:
scala> strings.sliding (3, 2) .map (_.mkString).toArray
res20: Array[String] = Array(123, 345, 567)
take 3, but forward 2
scala> strings.sliding (2, 3) .map (_.mkString).toArray
res21: Array[String] = Array(12, 45, 7)
take 2, forward 3 (thereby skipping every third)