Find format string from a set of printf formatted strings - printf

Say I have a set of strings formatted with a printf for example:
printf("Name: %s, Age: %d, code:0x%08x\t\n",...);
Now I dont know the format string of the printf but have instances of the output call it a set of outputs example:
"Name: Brian Bach, Age: 24, code:0x00123456"
"Name: Julian Roter, Age: 32, code:0x00000456"
The task is to find the format_string that created this. Obviously there are many format strings that can produce this output but how to find the minimal format string (roughly minimum number of % specifiers)..

Related

How to replace the list of decimal numbers in a string with another list of decimal numbers?

I need to replace the list of decimal numbers in a string with another list of decimal numbers. The following is a first try, that changes all decimal numbers with the same decimal number:
>>> re.sub (r"[-+]?\d*\.\d+f?", "1.0", "hello 1.2 3.4")
'hello 1.0 1.0'
I need something like my_replace below:
>>> my_replace (r"[-+]?\d*\.\d+f?", [1.0, 2.0], "hello 1.2 3.4")
'hello 1.0 2.0'
How can i implement my_replace with python's re module?
I don't think that you can use a list as replacement variables and iterate over them. So it can't handle unhashable objects (this is what python is complaining about). But it wouln'd be able to handle numerics as well (so it would need a list of strings but this is obviously hypothetical xD)
I would just loop over the string and compy everything that is not a decimal number to a new string and replacing the decimal numbers found.
text = "hello 1.2 3.4 don't replace an integer: 9 but this decimal number is too much: 0.0 (so use last value!)"
new_numbers = [42, 3.1415926535]
new_text = ''
idx_last = 0
for i, tx in enumerate(re.finditer(r'[-+]?\d*\.\d+f?', text)):
# add text before the number
new_text += tx.string[idx_last:tx.start()]
# add new number (but ensure that your are not overflowing the list of new numbers
new_text += str(new_numbers[min([i, len(new_numbers) - 1])])
# update text index
idx_last = tx.end()
# update remaining part of the text
new_text += text[idx_last:]
"hello 42 3.1415926535 don't replace an integer: 9 but this decimal number is too much: 3.1415926535 (so use last value!)"
Wrap it to a function and you have your my_replace() =)

The as.Date() Function does not work, my characters remain characters

I have an Excel File in which there is a column containing the date and hour of a regarding measurment in the format 01.01.2018 01:00.
The first 3 rows contain characters, the whole column is formatted as "Number" (in Excel/libre)
If I try to read the xlxs file with readxl:
NO2_2018 <- read_excel("NO2_2018.xlsx", sheet = "Seite 1",
range = "A2:AU8762", col_types = c("date",
"numeric", ....)
I get NA Values (format is POSIXct) and the warning
Expecting date in .... / .....: got '03.01.2018 02:00'
Then I thought I read it as "txt" and then convert it with as.Date() function:
as.Date(NO2_2018$Zeitpunkt,format = "%d.%m.%Y% H:%M", tz="CEST")
However, it does not change the class
class(NO2_2018$Zeitpunkt)
[1] "character"
Have you tried to change the dot in the date and then use the as.date in your transformed variable?
(gsub(".", "/", date)

Bitwise "and" operation into the kotlin

If negative number is from -31 to -1 then I would like represent it into the format 111XXXXX.
I try to do it using "and" bitwise operator:
println("0b00011111 & 0xe0 is ${0b00011111 and 0xe0}")
println("31 & 0xe0 is ${31 and 0xe0}")
println("0b00011111 & 0b11100000 is ${0b00011111 and 0b11100000}")
But the result is always 0. Where did I make the mistake?
It prints 0 because 00011111 and 11100000 always returns 0. The return type of and is Int, so if you want to print it in base 2 with leading zeroes, you have to format it.
To convert it to a String in base 2, you can call the toString method on Int with a radix parameter:
val numberString = (0b00011111 and 0b11100000).toString(2);
This will give you the number in binary format, but without leading zeroes. You need to left-pad with zeroes to get the format you want. I leave that task up to you (hint: padStart) ;)

Format a number to display a comma when larger than a thousand

I am writing some code in Visual Basic.net and have a question.
If I have a long number, that is larger than 1000, how can I format this value to be 1,000 (with a comma) and for this to be stored in a string?
For e.g.
1234 will be stored as 1,234
12345 will be stored as 12,345
123456 will be stored as 123,456
Is this done with a TryParse statement?
May I have some help to so this?
Take a look at The Numeric ("N") Format Specifier
General use:
Dim dblValue As Double = -12445.6789
Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
' Displays -12,445.68
If you are only using integers then the following:
Dim numberString As String = 1234.ToString("N0")
Will show numberString = "1,234" as the "N0" format will not add any figures after a decimal point.
For those wanting to do a currency with commas and decimals use the following: .ToString("$0,00.00")
Using $ notation:
int myvar = 12345;
Console.WriteLine($"Here is my number: {myvar:N0}");

Format number to string

I have to format number to exact this format "###,###,###.##" and try to write program like this:
Dim myNum as double = 1255.32
Debug.Print(myNum.ToString("###,###,###.##"))
I can do .PadLeft to ensure aligning with fixed-width font but my number is not showed correctly with this format string.
If I write that by using "##0.00" then I haven't thousand separator showed.
In earlier Basic versions that was easy but...
How to get number showed in this format in VB.NET?
If is important my local decimal "point" is "," (comma).
Example:
First source Second source
---------------- ----------------
Price: 97.419,52 97.419,26
Tax: 4.870,98 4.870,96
Brutto: 102.290,50 102.290,24
Temp source
----------------
Price: 0,00
Tax: 0,00 Difference
Brutto: 0,00 - 0,26
CultureInfo gives you much more control on the number format. Sample code:
Dim culture As Globalization.CultureInfo = New Globalization.CultureInfo(Globalization.CultureInfo.CurrentCulture.Name)
culture.NumberFormat.NumberDecimalSeparator = "."
culture.NumberFormat.NumberGroupSeparator = ","
Dim myNum As Double = 1255.32
Debug.Print(myNum.ToString("N", culture))