I have a Textbox1.Text like that: 1784397425
How can I get the 2 combinations or 1 combination out of here?
this is a model:
Like 17 78 84 43 39 97 74 42 25 1 7 8 4 3 9 7 4 2 5
That's another model:
It can also be like this: (first digit 1, we take 1 by 7, then 1 by 8 and so on.)
17 18 14 13 19 17 14 15
and so on...
78 74 73 79 77 74 72 75
84 83 89 87 84 82 85
43 49 47 44 42 45
39 37 34 32 35
and so on...
any such model algorithm would help me a lot either of these two.
Code 3: unfortunately this doesn't work very well.
Since they're strings to begin with you can use.
Left(str,n) n is the number of left characters you want.
Right(str,n) n is the number of Right characters you want.
Mid(str,n,nn) Mid is like substring where n is the starting char, and nn is the number from start you want from the str.
Dim line1 = "47"
Dim d1 As String = Left(line1,1) 'this is 4
Dim d2 As String = Right(line1,1) 'this is 7
now the math
Dim a1 As Integer = Int(d1) + Int(d2)
or
Dim a1 As Integer = CInt(d1) + CInt(d2)
As suggested by Andrew, use Substring to extract the portion of the string that you want.
I've wrapped that in an iterator that returns all the substrings:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox2.Text = String.Join(" ", Substrings(TextBox1.Text, 2))
End Sub
Public Iterator Function Substrings(ByVal source As String, ByVal length As Integer) As IEnumerable(Of String)
If length >= 1 AndAlso length <= source.Length Then
For i As Integer = 0 To (source.Length - length)
Yield source.Substring(i, length)
Next
End If
End Function
If you want the user to set the value of the length parameter, then drop a NumericUpDown control on your form and grab the .Value from that.
Related
I need to split a string of 20 digits individually and multiply each single one by a value.
String (this string is in a text box) example: 11604999123400002586
After the split, I must do the following operation:
51 x 1º digit +
73 x 2º digit +
17 x 3º digit +
89 x 4º digit +
38 x 5º digit +
62 x 6º digit +
45 x 7º digit +
53 x 8º digit +
15 x 9º digit +
50 x 10º digit +
5 x 11º digit +
49 x 12º digit +
34 x 13º digit +
81 x 14º digit +
76 x 15º digit +
27 x 16º digit +
90 x 17º digit +
9 x 18º digit +
30 x 19º digit +
3 x 20º digit
In this example:
Result = 51x1 + 73x1 + 17x6 + 89x0 + 38x4 + 62x9 +
45x9 + 53x9 + 15x1 + 50x2 + 5x3 + 49x4 + 34x0 + 81x0 + 76x0 + 27x0 + 90x2
+ 9x5 + 30x8 + 3x6 = 2627
What would be the best way to do it?
In VBA you can do something like this:
Dim a(20) as Integer
a(1) = 51
a(2) = 73
…
a(20) = 3
Dim Result AS Long: Result = 0
For i = 1 to 20
Result = Result + CInt(Mid(MyString,i,1)) * a(i)
Next i
You can do it with SQL.
Create a table, say with name weights, with 2 columns:
id: Number
weight: Number
and insert these rows:
id weight
1 51
2 73
3 17
4 89
5 38
6 62
7 45
8 53
9 15
10 50
11 5
12 49
13 34
14 81
15 76
16 27
17 90
18 9
19 30
20 3
Then execute this query:
SELECT SUM(w.weight * val(mid('11604999123400002586', w.id, 1))) AS result
FROM weights AS w
Result:
2627
Consistency of structure is critical in string manipulation. Assume there will always be 20 digits in string and every record will have value.
Calculate 20 columns and add them together for the Total - here are 2:
SELECT Quotes.*, Mid([Data],1,1)*53 AS A, Mid([Data],2,1)*71 AS B, [A]+[B] AS Total FROM Quotes;
Could pull weight factor from table. DLookup is one way but domain aggregate can cause slow performance in large dataset so if weight factors will never change, probably don't want this:
SELECT Quotes.Data, Mid([Data],1,1)* DLookup("Weight","Factors","ID=1") AS A, Mid([Data],2,1)* DLookup("Weight","Factors","ID=2") AS B, [A]+[B] AS Total FROM Quotes;
A Cartesian relationship with aggregate calcs query using data and weight factors tables can calculate Total - Cartesian can also perform slowly with large dataset:
SELECT Quotes.ID, Sum(Mid([Data],[Factors].[ID],1)*[Weight]) AS Total
FROM Factors, Quotes
GROUP BY Quotes.ID;
A custom VBA function could return just the total. Call this function from query or textbox. The weight factors can be pulled from table or hard coded in an array construct.
Function GetTotal(strD) As Long
Dim rs As DAO.Recordset, x As Integer
Set rs = CurrentDb.OpenRecordset("SELECT Weight FROM Factors ORDER BY ID")
For x = 1 To 20
GetTotal = GetTotal + Mid(strD, x, 1) * rs!Weight
rs.MoveNext
Next
End Function
[Updated] Thank you all for your guidance.
I have modified the code as below.
But the macro did not run, nor generated any errors.
I did not want to double quote each item in the variable "columns" either.
Thank you very much for any helps.
Thanks!
My code is as below:
I would like to generate 35 random columns index if the sample size is 80,
and if the sample size is 50 then generate 25 and if the sample size is 32 then generate 20 and if the sample size is 20 then generate 15.
And then created a average and sd columns for that.
`
Sub randomize1()
Dim wb As Workbook
Dim average As Range
Dim sd As Range
Dim columns As String
Dim values As Variant
Set wb = ActiveWorkbook
With wb
For i = 2 To 1730
Set average = Worksheets("CT").Range("CY2:CY1730")
Set sd = Worksheets("CT").Range("CZ2:CZ1730")
If WorksheetFunction.CountA(".Cells(i,11):.Cells(i,91)") = 80 Then
columns = "17 21 31 32 2 18 22 7 9 20 23 6 27 10 26 8 29 3 1 13 5 24 35 15 28 11 25 14 16 4 12 34 19 30 33"
values = Strings.Split(columns, " ")
.Cells(i, average).Value = Application.average(Range(i, columns).Offset(, 10).Select)
.Cells(i, sd).Value = Application.WorksheetFunction.StDev(Range(i, columns).Offset(, 10).Select)
ElseIf WorksheetFunction.CountA(".Cells(i,11):.Cells(i,91)") = 50 Then
columns = "1 22 17 5 18 8 20 9 10 6 25 14 13 7 2 3 19 16 4 12 15 11 24 23 21"
values = Strings.Split(columns, " ")
.Cells(i, average).Value = Application.average(Range(i, columns).Offset(, 10).Select)
.Cells(i, sd).Value = Application.WorksheetFunction.StDev(Range(i, columns).Offset(, 10).Select)
ElseIf WorksheetFunction.CountA(".Cells(i,11):.Cells(i,91)") = 32 Then
columns = "14 2 3 16 19 11 20 1 13 18 6 9 17 8 4 5 10 15 12 7"
values = Strings.Split(columns, " ")
.Cells(i, average).Value = Application.average(Range(i, columns).Offset(, 10).Select)
.Cells(i, sd).Value = Application.WorksheetFunction.StDev(Range(i, columns).Offset(, 10).Select)
ElseIf WorksheetFunction.CountA(".Cells(i,11):.Cells(i,91)") = 20 Then
columns = "13 8 7 2 1 12 11 6 14 15 3 10 4 5 9"
values = Strings.Split(columns, " ")
.Cells(i, average).Value = Application.average(Range(i, columns).Offset(, 10).Select)
.Cells(i, sd).Value = Application.WorksheetFunction.StDev(Range(i, columns).Offset(, 10).Select)
End If
Next i
End With
End Sub
``
Dim columns As String
columns = (17 21 31 32 2 18 22 7 9 20 23 6 27 10 26 8 29 3 1 13 5 24 35 15 28 11 25 14 16 4 12 34 19 30 33)`
The expression (17 21 31 ...) isn't a String literal. In VBA (as in many other languages), string literals are delimited with " double quotes, not parentheses.
The compile error is because the expression (integerLiteral integerLiteral integerLiteral ...) can't be evaluated, it means nothing to the compiler. I'm guessing it's choking at the 21 token, especting a , list separator or a ) closing parenthesis, since columns = (17) would be a valid expression.
This would compile:
Dim columns As String
columns = "17 21 31 32 2 18 22 7 9 20 23 6 27 10 26 8 29 3 1 13 5 24 35 15 28 11 25 14 16 4 12 34 19 30 33"
...but then, it's not an array of values, it's just a string. There's a Split function in the VBA.Strings module that you can use to split a string into an array, given a separator - like, a space character:
Dim values As Variant
values = Strings.Split(columns, " ")
That gives you an array of Variant/String items that you can later iterate and convert to Long values if you need to.
in R, I can use c(1,2,3) if I am going to define an object,column index as (1,2,3)
That statement is extremely unclear and hard to understand for an audience of VBA experts - how does (1,2,3) map to object, column index? Is 1 an "object"? Is 2,3 a column index? Or did you mean object, column, index? Doesn't make much sense, even to someone that knows what a value tuple is. Sounds like R lets you define value tuples inline. That's cool, but there's no concept of a value tuple in VBA, and no inline objects.
If you need an object, you add a class module to your project, and define it - at a bare minimum, using public fields to define its public interface:
'Class1
Option Explicit
Public Value As Long
Public Column As Long
Public Index As Long
Then you can create new instances of this class using New: Set foo = New Class1, and then assign and read its properties, invoke its methods: Debug.Print foo.Index, foo.Column, foo.Value.
About this:
columns = (num & num& ... &num)
Note that some tokens have multiple syntactical purposes; & is one such token. When surrounded with spaces as in num & num, it's the string concatenation operator.
But without a leading space, as in num&, it's a type hint character that tells the compiler num is a Long integer, hence the syntax error; num& num is just as illegal as (42 17).
In VBA, everything between quotes "" is a String.
If you simply need to create a string containing those numbers, you can do this:
Dim columns As String
columns = "17 21 31 32 2 18 22 7 9 20 23 6 27 10 26 8 29 3 1 13 5 24 35 15 28 11 25 14 16 4 12 34 19 30 33"
If you need to process those numbers individually, you need an array, which you can initialize following multiple ways.
Classic Way
Declare your Array with a fixed size and then assingn it the values you want:
Dim myArray(1 to 34) As String 'Goes from 1 to 34
myArray(1) = 17 'First element
myArray(2) = 21 'Second element
'etc...
Use Array() Command
You can use the useful command Array() to initialize an Array with an inline command:
Dim myArray() As Variant
myArray = Array("17", "21", "31", "32", "2")
Note that your variable must be Variant to use this solution.
Hope this helps.
Whats the most effective method to detect n Pattern in consecutive numbers?
Maybe an SQL column or vector, R.
Some Pseudocode -R- to illustrate the "problem":
find Pattern in consecutive integers, where
2nd integer < 1st integer,
3rd integer > 2nd integer &
4th integer > 3rd integer.
a <- x
b <- x +1 < a
c <- x +2 > b
d <- x +3 > c
pattern <- c(a, b, c, d)
example: pattern <- c(10, 8, 9, 11) or pattern <- c(2.11, 2.08, 2.09, 2.11)
count(pattern)
find(pattern)
If you take the difference of the vector then the first should be negative and the other two positive, so,
a <- c(10, 8, 9, 11)
all((diff(a) < 0) == c(TRUE, FALSE, FALSE))
#[1] TRUE
To apply that to a bigger vector, you can use rollapply from zoo package, i.e.
library(zoo)
a <- sample(1:100,100,replace=T)
unique(rollapply(a, 4, by = 1, function(i) i[all((diff(i) < 0) == c(TRUE, FALSE, FALSE))]))
which gives,
[,1] [,2] [,3] [,4]
[1,] 85 18 85 92
[2,] 44 27 67 76
[3,] 58 2 39 54
[4,] 85 69 82 84
[5,] 61 4 40 44
[6,] 65 58 73 97
[7,] 19 9 92 96
[8,] 33 24 57 73
[9,] 79 11 37 100
I want to learn vernam encryption.
First of all, can you confirm me that the algorithm is the same for encoding and decoding?
I have read an exercice which say to decode this message with Pi:
01237 55235 31127 12189 87479 1592
I have tried vernam python pacakge and i tried this:
py_vernam.vernam('01237552353112712189874791592','3.141592653589793238462643383')
or
py_vernam.vernam('01237552353112712189874791592','31415926535897932384626433832')
But it does not give me a readable message...
Thanks
"can you confirm me that the algorithm is the same for encoding and decoding?"
-> first of all, we are talking about encryption not encoding (at the first step) ... the notable difference, to tell those two apart, is that there is a key involved here...
depending on which variant of vernam you are handling, encryption and decryption may be the same or not ... for the binary variant it surely is the same operation, a simple XOR
if you happen to have got your fingers on the "let's do this by hand" or schoolbok variant, it is not, basically because it handles values mod 10 and not mod 2, leading to encryption is + ... decryption is - ...
the notation in blocks of 5 is an indication for the mod 10 variant, since with the mod 2 variant you usually just handle binary data
01237552353112712189874791592 Ciphertext
31415926535897932384626433832 Key
========================================
70822636828325880805258368760 Text (encoded)
so finally we have to read the encoding into characters... (second step)
it is up to the user of the cipher to give these numbers a meaning so when searching for your example message you can find a french page showing a substitution table... so ... let's have a look ...
Substitution table:
Clair A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Chiffré 6 38 32 4 8 30 36 34 39 31 78 72 70 76 9 79 71 58 2 0 52 50 56 54 1 59
Result:
70 8 2 2 6 36 8 2 8 32 58 8 0 8 0 52 58 36 8 76 0
M E S S A G E S E C R E T E T U R G E N T
Any idea why ISNUMERIC('0D0') = 1 is TRUE in SQL Server 2008?
I'm validating ID numbers from another system, which sometimes contain letters that we don't want to bring over, but this combination is tripping up the code (the specific ID that it thinks is numeric is "005406257D6"). Downstream we're doing CONVERT(BIGINT, #val), which is obviously choking when it finds these "D" values.
What special case am I hitting, and how do I account for it?
Basically, IsNumeric is useless - all it verifies is that the string can be converted to any of the numeric types. In this case, "0D0" can be converted to a float. (I can't remember the reason, but effectively "D" is a synonym for "E", and means it's scientific notation):
select CONVERT(float,'2D3')
2000
If you want to verify that it's just digits, then not #val like '%[^0-9]%' would be a better test. You can improve this further by adding a length check also.
An good explanation of why.
http://www.sqlservercentral.com/articles/IsNumeric/71512/
(below is #RedFilter's more exhaustive query/list)
SELECT [Ascii Code] = STR(Number)
,[Ascii Character] = CHAR(Number)
,[ISNUMERIC Returns] = ISNUMERIC(CHAR(Number))
FROM Master.dbo.spt_Values
WHERE Type = 'P'
AND Number BETWEEN 0 AND 255
AND ISNUMERIC(CHAR(Number)) = 1
UNION
SELECT [Ascii Code] = STR(Number)
,[Ascii Character] = CHAR(Number)
,[ISNUMERIC Returns] = ISNUMERIC('0' + CHAR(Number) + '0')
FROM Master.dbo.spt_Values
WHERE Type = 'P'
AND Number BETWEEN 0 AND 255
AND ISNUMERIC('0' + CHAR(Number) + '0') = 1
yeilds
Ascii Code Ascii Character
---------- ---------------
0
9
10
11
12
13
36 $
43 +
44 ,
45 -
46 .
48 0
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9
68 D
69 E
92 \
100 d
101 e
128 €
160
162 ¢
163 £
164 ¤
165 ¥