How can I write a conditional without any conditional statements or operators?

Vedanshu

My computer teacher wants me to write code to concatenate two strings( in a weird way). The code should be such that if length of both string are equal then output should be (string1 + string2). Otherwise the output should be string which is larger in length. Challenge is that I should not use if else statements or condition?exp1:exp2 whatsoever. This is what I am able to come up with (a and b are names of input string):

int aLen = a.Length;
int bLen = b.Length;
//+1 is added to lengths to prevent divide by zero 
int bGreatFlag = ((aLen+1) % (bLen + 1)) / (aLen + 1);  //1 if aLen < bLen; 0 Otherwise
int aGreatFlag = ((bLen+1) % (aLen+1)) / (bLen+1);  //1 if bLen < aLen; 0 Otherwise
string result = (a + b).Substring((bGreatFlag) * aLen,(aLen + bLen)-(bGreatFlag*aLen)-(aGreatFlag*bLen));

I believe that there is another way to approach this question which I am missing altogether(an inbuilt function or some LINQ maybe?). Any other approach or any pointers in the right direction to join strings conditionally will be really helpful. Thanks :) . Please be kind if the answer to this is very trivial.

sloth

Since you're allowed to use LINQ, here's a possible solution:

But your strings into a collection, group it by the length of its string, order the result by the length of the strings, then take the group with the longest strings. Since now you have a collection of either both strings (if they are of equal length) or the longer one, create a string of this collection by using String.Join.

Spoiler (don't miss the fun of implementing this yourself):

var result = String.Join("", new[]{a, b}.GroupBy(x => x.Length).OrderByDescending(x => x.Key).First().ToArray());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I write a conditional without any conditional statements or operators?

From Dev

How can I DRY this series of conditional statements?

From Dev

Rewriting a piece of C code without conditional statements or operators?

From Dev

Rewriting a piece of C code without conditional statements or operators?

From Dev

How can I use JavaDocs in Eclipse to show conditional return statements?

From Dev

How to write conditional import statements in QML?

From Dev

how to write conditional statements in ng-class?

From Dev

Oracle SQL - How can I write an insert statement that is conditional and looped?

From Dev

Oracle SQL - How can I write an insert statement that is conditional and looped?

From Dev

How do I simplify a conditional with multiple or statements?

From Dev

How can I use a conditional operator in printf but without the "else" part?

From Dev

How to write a conditional operator (?:) without using else

From Dev

how to write a coin counter function with only conditional statements in JavaScript?

From Dev

How to set class invariants without using conditional if statements

From Dev

Conditional Operators

From Dev

How to write conditional css?

From Dev

How can remove conditional statements while adding the common responsibility to the class?

From Java

How to shorten my conditional statements

From Dev

Change comparison operators without large conditional block

From Dev

How can I have a conditional on cabal?

From Dev

How can I have conditional out parameters

From Dev

How I can use "case" conditional in Scheme

From Dev

How can I check for a value in a conditional array?

From Dev

Django conditional queries: How can I avoid this?

From Dev

Haskell: Can apply view pattern on conditional operators

From Java

How can I add an if statement to display conditional HTML using PHP without using the echo function

From Dev

How can I use VBA to format Symbols / Icons into cells without using conditional formatting

From Dev

How can I write regex for the following statements?

From Dev

Vuex: Can mutations contain conditional statements?

Related Related

  1. 1

    How can I write a conditional without any conditional statements or operators?

  2. 2

    How can I DRY this series of conditional statements?

  3. 3

    Rewriting a piece of C code without conditional statements or operators?

  4. 4

    Rewriting a piece of C code without conditional statements or operators?

  5. 5

    How can I use JavaDocs in Eclipse to show conditional return statements?

  6. 6

    How to write conditional import statements in QML?

  7. 7

    how to write conditional statements in ng-class?

  8. 8

    Oracle SQL - How can I write an insert statement that is conditional and looped?

  9. 9

    Oracle SQL - How can I write an insert statement that is conditional and looped?

  10. 10

    How do I simplify a conditional with multiple or statements?

  11. 11

    How can I use a conditional operator in printf but without the "else" part?

  12. 12

    How to write a conditional operator (?:) without using else

  13. 13

    how to write a coin counter function with only conditional statements in JavaScript?

  14. 14

    How to set class invariants without using conditional if statements

  15. 15

    Conditional Operators

  16. 16

    How to write conditional css?

  17. 17

    How can remove conditional statements while adding the common responsibility to the class?

  18. 18

    How to shorten my conditional statements

  19. 19

    Change comparison operators without large conditional block

  20. 20

    How can I have a conditional on cabal?

  21. 21

    How can I have conditional out parameters

  22. 22

    How I can use "case" conditional in Scheme

  23. 23

    How can I check for a value in a conditional array?

  24. 24

    Django conditional queries: How can I avoid this?

  25. 25

    Haskell: Can apply view pattern on conditional operators

  26. 26

    How can I add an if statement to display conditional HTML using PHP without using the echo function

  27. 27

    How can I use VBA to format Symbols / Icons into cells without using conditional formatting

  28. 28

    How can I write regex for the following statements?

  29. 29

    Vuex: Can mutations contain conditional statements?

HotTag

Archive