[.NET / C#] Genius Haggle / SmartHaggle

Discussion in 'Code Snippets and Tutorials' started by Lightning, Jul 25, 2011.

  1. Lightning

    Lightning Administrator
    Staff Member

    Joined:
    Nov 8, 2008
    Messages:
    3,021
    Likes Received:
    195
    Gender:
    Male
    Location:
    Florida, USA
    Quick SmartHaggle


    C#:
    Code (Text):
    1.  
    2. /// <summary>
    3. /// Generates and returns a string that repeats a specified number of first characters in a string for the length of that string. Useful for haggling, especially on Neopets.
    4. /// </summary>
    5. /// <param name="strPrice">Required. The string 'price' to haggle.</param>
    6. /// <param name="iLen">Optional. The number of characters to repeat.</param>
    7. /// <returns></returns>
    8. public string QuickSmartHaggle(string strPrice, byte iLen = 2)
    9. {
    10. return (strPrice.Length > iLen ? string.Join("", Enumerable.Repeat(strPrice.Substring(0, iLen), (int)((strPrice.Length / iLen) + 1))).Substring(0, strPrice.Length) : strPrice);
    11. }
    12.  

    Visual Basic .NET:
    Code (Text):
    1.  
    2. ''' <summary>
    3. ''' Generates and returns a string that repeats a specified number of first characters in a string for the length of that string. Useful for haggling, especially on Neopets.
    4. ''' </summary>
    5. ''' <param name="strPrice">Required. The string 'price' to haggle.</param>
    6. ''' <param name="iLen">Optional. The number of characters to repeat.</param>
    7. ''' <returns></returns>
    8. Public Function QuickSmartHaggle(strPrice As String, Optional iLen As Byte = 2) As String
    9. Return (If(strPrice.Length > iLen, String.Join("", Enumerable.Repeat(strPrice.Substring(0, iLen), CInt((strPrice.Length \ iLen) + 1))).Substring(0, strPrice.Length), strPrice))
    10. End Function
    11.  


    This function is pretty simple and and done in 1 line. So if iLen = 2 and strPrice = 23019, it will return 23232. If you're looking for a simple haggle, this is it.






    Genius Haggle



    C#:
    Code (Text):
    1.  
    2. /// <summary>
    3. /// Generates a haggled price based on a specified price. For the more 'convenient' prices, the maximum range parameter, 'iRange', is used.
    4. /// </summary>
    5. /// <param name="strPrice">Required. The price of which to haggle off of.</param>
    6. /// <param name="iRange">Optional. For more advanced 'convenient' prices, this value determines the maximum absolute value of the difference between haggled price and the original, specified price.</param>
    7. /// <returns></returns>
    8. public string GeniusHaggle(string strPrice, uint iRange = 500)
    9. {
    10. strPrice = strPrice.Replace(",", "");
    11. strPrice = strPrice.Replace(" ", "");
    12. strPrice = strPrice.Replace(".", "");
    13. int iPrice = Convert.ToInt32(strPrice);
    14. int strPriceLength = strPrice.Length;
    15.  
    16.  
    17.  
    18.  
    19. if (strPriceLength < 1)
    20. return string.Empty;
    21.  
    22.  
    23. else if (strPriceLength <= 2)
    24. return ((int)(iPrice * 0.98)).ToString();
    25.  
    26.  
    27. else if ((strPriceLength == 3) | (strPriceLength == 4))
    28. {
    29. string strPrice0 = strPrice[0].ToString();
    30. int iPrice1 = Convert.ToInt32(strPrice.Substring(1, 1));
    31.  
    32.  
    33. if (Math.Abs(iPrice - Convert.ToInt32(RepeatString(strPrice0, strPriceLength))) <= iRange)
    34. return RepeatString(strPrice0, strPriceLength);
    35.  
    36. else if (iPrice1 == 0)
    37. {
    38. strPrice0 = (Convert.ToInt32(strPrice0) - 1).ToString();
    39. return strPrice0 + "9" + strPrice0 + (strPriceLength == 4 ? "9" : "");
    40. }
    41. else return strPrice0 + (iPrice1 - 1).ToString() + strPrice0 + (strPriceLength == 4 ? (iPrice1 - 1).ToString() : "");
    42. }
    43. else
    44. {
    45. string strPrice0 = strPrice[0].ToString();
    46. string strPrice1 = strPrice[1].ToString();
    47. int iPrice0 = Convert.ToInt32(strPrice0);
    48. int iPrice1 = Convert.ToInt32(strPrice1);
    49.  
    50.  
    51.  
    52.  
    53. int iRepeat = (int)(strPriceLength / 2) + 1;
    54.  
    55.  
    56. if (Math.Abs(iPrice - (Convert.ToInt32(RepeatString(strPrice0, strPriceLength)))) <= iRange) //XXXXX...
    57. {
    58. return RepeatString(strPrice0, strPriceLength);
    59. }
    60. else if ((iPrice0 > 1) && (Math.Abs(iPrice - (Convert.ToInt32(RepeatString((iPrice0 - 1).ToString(), strPriceLength)))) <= iRange)) //-1 XXXXX...
    61. {
    62. return RepeatString((iPrice0 - 1).ToString(), strPriceLength);
    63. }
    64. else if (Math.Abs(iPrice - Convert.ToInt32(RepeatString(strPrice0 + strPrice1, iRepeat).Substring(0, strPriceLength))) <= iRange) //XYXYX...
    65. {
    66. return RepeatString(strPrice0 + strPrice1, iRepeat).Substring(0, strPriceLength);
    67. }
    68. else if ((iPrice1 > 0) && (Math.Abs(iPrice - Convert.ToInt32(RepeatString(strPrice0 + (iPrice1 - 1).ToString(), iRepeat).Substring(0, strPriceLength))) <= iRange)) //-1 XYXYX...
    69. {
    70. return RepeatString(strPrice0 + (iPrice1 - 1).ToString(), iRepeat).Substring(0, strPriceLength);
    71. }
    72. else if (Math.Abs(iPrice - Convert.ToInt32(strPrice0 + strPrice1 + RepeatString((iPrice0 < iPrice1 ? strPrice0 : strPrice1), strPriceLength - 2))) <= iRange) //XYSSS... (smaller)
    73. {
    74. return strPrice0 + strPrice1 + RepeatString((iPrice0 > iPrice1 ? strPrice0 : strPrice1), strPriceLength - 2);
    75. }
    76. else if (Math.Abs(iPrice - Convert.ToInt32(strPrice0 + strPrice1 + RepeatString((iPrice0 > iPrice1 ? strPrice0 : strPrice1), strPriceLength - 2))) <= iRange) //XXLLL... (larger)
    77. {
    78. return strPrice0 + strPrice1 + RepeatString((iPrice0 < iPrice1 ? strPrice0 : strPrice1), strPriceLength - 2);
    79. }
    80. else if ((iPrice1 > 0) && Math.Abs(iPrice - Convert.ToInt32(strPrice0 + (iPrice1 - 1).ToString() + RepeatString((iPrice0 > iPrice1 - 1 ? strPrice0 : strPrice1), strPriceLength - 2))) <= iRange) //X(-1Y)SSS... (smaller)
    81. {
    82. return strPrice0 + (iPrice1 - 1).ToString() + RepeatString((iPrice0 < iPrice1 ? strPrice0 : strPrice1), strPriceLength - 2);
    83. }
    84. else if ((iPrice1 > 0) && Math.Abs(iPrice - Convert.ToInt32(strPrice0 + (iPrice1 - 1).ToString() + RepeatString((iPrice0 < iPrice1 - 1 ? strPrice0 : strPrice1), strPriceLength - 2))) <= iRange) //X(-1Y)LLL... (larger)
    85. {
    86. return strPrice0 + (iPrice1 - 1).ToString() + RepeatString((iPrice0 > iPrice1 ? strPrice0 : strPrice1), strPriceLength - 2);
    87. }
    88. else
    89. {
    90. int iPrice3 = Convert.ToInt32(strPrice[3].ToString());
    91. iPrice -= (iPrice > 0 ? 1 : 0);
    92. return strPrice0 + strPrice1 + RepeatString(strPrice[2].ToString() + iPrice3.ToString(), iRepeat).Substring(0, strPriceLength - 2);
    93. }
    94. }
    95. }
    96.  
    97. /// <summary>
    98. /// Returns a string that contains a specified string repeated a specified amount of times.
    99. /// </summary>
    100. /// <param name="strRepeat">Required. The string to repeat.</param>
    101. /// <param name="iCount">Required. The value indicating how many times to repeat the parameter 'strRepeat'.</param>
    102. /// <returns></returns>
    103. public string RepeatString(string strRepeat, int iCount)
    104. {
    105. return string.Join("", Enumerable.Repeat(strRepeat, iCount));
    106. }
    107.  
    108.  


    Visual Basic .NET:
    Code (Text):
    1.  
    2. ''' <summary>
    3. ''' Generates a haggled price based on a specified price. For the more 'convenient' prices, the maximum range parameter, 'iRange', is used.
    4. ''' </summary>
    5. ''' <param name="strPrice">Required. The price of which to haggle off of.</param>
    6. ''' <param name="iRange">Optional. For more advanced 'convenient' prices, this value determines the maximum absolute value of the difference between haggled price and the original, specified price.</param>
    7. ''' <returns></returns>
    8. Public Function GeniusHaggle(strPrice As String, Optional iRange As UInteger = 500) As String
    9. strPrice = strPrice.Replace(",", "")
    10. strPrice = strPrice.Replace(" ", "")
    11. strPrice = strPrice.Replace(".", "")
    12. Dim iPrice As Integer = Convert.ToInt32(strPrice)
    13. Dim strPriceLength As Integer = strPrice.Length
    14.  
    15.  
    16.  
    17.  
    18. If strPriceLength < 1 Then
    19. Return String.Empty
    20.  
    21.  
    22. ElseIf strPriceLength <= 2 Then
    23. Return CInt(Math.Truncate(iPrice * 0.98)).ToString()
    24.  
    25.  
    26. ElseIf (strPriceLength = 3) Or (strPriceLength = 4) Then
    27. Dim strPrice0 As String = strPrice(0).ToString()
    28. Dim iPrice1 As Integer = Convert.ToInt32(strPrice.Substring(1, 1))
    29.  
    30.  
    31. If Math.Abs(iPrice - Convert.ToInt32(RepeatString(strPrice0, strPriceLength))) <= iRange Then
    32. Return RepeatString(strPrice0, strPriceLength)
    33.  
    34.  
    35. ElseIf iPrice1 = 0 Then
    36. strPrice0 = (Convert.ToInt32(strPrice0) - 1).ToString()
    37. Return strPrice0 & "9" & strPrice0 & (If(strPriceLength = 4, "9", ""))
    38. Else
    39. Return strPrice0 & (iPrice1 - 1).ToString() & strPrice0 & (If(strPriceLength = 4, (iPrice1 - 1).ToString(), ""))
    40. End If
    41. Else
    42. Dim strPrice0 As String = strPrice(0).ToString()
    43. Dim strPrice1 As String = strPrice(1).ToString()
    44. Dim iPrice0 As Integer = Convert.ToInt32(strPrice0)
    45. Dim iPrice1 As Integer = Convert.ToInt32(strPrice1)
    46.  
    47.  
    48.  
    49.  
    50. Dim iRepeat As Integer = CInt(strPriceLength \ 2) + 1
    51.  
    52.  
    53. If Math.Abs(iPrice - (Convert.ToInt32(RepeatString(strPrice0, strPriceLength)))) <= iRange Then
    54. 'XXXXX...
    55. Return RepeatString(strPrice0, strPriceLength)
    56. ElseIf (iPrice0 > 1) AndAlso (Math.Abs(iPrice - (Convert.ToInt32(RepeatString((iPrice0 - 1).ToString(), strPriceLength)))) <= iRange) Then
    57. '-1 XXXXX...
    58. Return RepeatString((iPrice0 - 1).ToString(), strPriceLength)
    59. ElseIf Math.Abs(iPrice - Convert.ToInt32(RepeatString(strPrice0 & strPrice1, iRepeat).Substring(0, strPriceLength))) <= iRange Then
    60. 'XYXYX...
    61. Return RepeatString(strPrice0 & strPrice1, iRepeat).Substring(0, strPriceLength)
    62. ElseIf (iPrice1 > 0) AndAlso (Math.Abs(iPrice - Convert.ToInt32(RepeatString(strPrice0 & (iPrice1 - 1).ToString(), iRepeat).Substring(0, strPriceLength))) <= iRange) Then
    63. '-1 XYXYX...
    64. Return RepeatString(strPrice0 & (iPrice1 - 1).ToString(), iRepeat).Substring(0, strPriceLength)
    65. ElseIf Math.Abs(iPrice - Convert.ToInt32(strPrice0 & strPrice1 & RepeatString((If(iPrice0 < iPrice1, strPrice0, strPrice1)), strPriceLength - 2))) <= iRange Then
    66. 'XYSSS... (smaller)
    67. Return strPrice0 & strPrice1 & RepeatString((If(iPrice0 > iPrice1, strPrice0, strPrice1)), strPriceLength - 2)
    68. ElseIf Math.Abs(iPrice - Convert.ToInt32(strPrice0 & strPrice1 & RepeatString((If(iPrice0 > iPrice1, strPrice0, strPrice1)), strPriceLength - 2))) <= iRange Then
    69. 'XXLLL... (larger)
    70. Return strPrice0 & strPrice1 & RepeatString((If(iPrice0 < iPrice1, strPrice0, strPrice1)), strPriceLength - 2)
    71. ElseIf (iPrice1 > 0) AndAlso Math.Abs(iPrice - Convert.ToInt32(strPrice0 & (iPrice1 - 1).ToString() & RepeatString((If(iPrice0 > iPrice1 - 1, strPrice0, strPrice1)), strPriceLength - 2))) <= iRange Then
    72. 'X(-1Y)SSS... (smaller)
    73. Return strPrice0 & (iPrice1 - 1).ToString() & RepeatString((If(iPrice0 < iPrice1, strPrice0, strPrice1)), strPriceLength - 2)
    74. ElseIf (iPrice1 > 0) AndAlso Math.Abs(iPrice - Convert.ToInt32(strPrice0 & (iPrice1 - 1).ToString() & RepeatString((If(iPrice0 < iPrice1 - 1, strPrice0, strPrice1)), strPriceLength - 2))) <= iRange Then
    75. 'X(-1Y)LLL... (larger)
    76. Return strPrice0 & (iPrice1 - 1).ToString() & RepeatString((If(iPrice0 > iPrice1, strPrice0, strPrice1)), strPriceLength - 2)
    77. Else
    78. Dim iPrice3 As Integer = Convert.ToInt32(strPrice(3).ToString())
    79. iPrice -= (If(iPrice > 0, 1, 0))
    80. Return strPrice0 & strPrice1 & RepeatString(strPrice(2).ToString() & iPrice3.ToString(), iRepeat).Substring(0, strPriceLength - 2)
    81. End If
    82. End If
    83. End Function
    84.  
    85. ''' <summary>
    86. ''' Returns a string that contains a specified string repeated a specified amount of times.
    87. ''' </summary>
    88. ''' <param name="strRepeat">Required. The string to repeat.</param>
    89. ''' <param name="iCount">Required. The value indicating how many times to repeat the parameter 'strRepeat'.</param>
    90. ''' <returns></returns>
    91. Public Function RepeatString(strRepeat As String, iCount As Integer) As String
    92.     Return String.Join("", Enumerable.Repeat(strRepeat, iCount))
    93. End Function
    94.  



    This function was one that I worked on for a little longer. I tried to make it so that it returns the most 'convenient' haggle price for a human. It looks like a lot of code and a lot to process, but most of the functions are pretty simple and it is mostly If statements. It usually takes less than 0 milliseconds, believe it or not, when using both the System.Diagnostics.Stopwatch and DateTime difference methods. It can be done a little more efficiently in VB because of the Case keyword, but C#'s switch does not allow a 'range' of numbers. And I was too lazy, so I just stuck it into a converter :p. If you guys have any suggestions, please do post. Appreciate any feedback :).
     
  2. Jaggerrr

    Jaggerrr Level IV

    Joined:
    Mar 11, 2007
    Messages:
    817
    Likes Received:
    10
    Location:
    Australia
    You're so hot right now
     
  3. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    Yep !
    Finally, I feel less alone XD