Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How would I generate a set of characters from a list of strings efficiently?
Suppose I've got a list of strings, e.g.:
windows
linux-2.6
Then the resulting string should be:
-.26dilnosuwx
The character order should correspond to the character type (ascii/utf8).
Programming language does not matter. I'd prefer a scripting solution (i.e. bash, python etc.) though.
The easiest to understand way (for me) would be:
create an array of 256 Boolean values
for each character in the string
convert the character to its numerical representation (i.e. 'A' is 65, etc.)
set the corresponding value in the array to true
end for
// done scanning strings. Now output:
for i = 0 to 255
if array[i] is set
output character value i
If you're working with Unicode characters, then that array will have to be 65,536 Boolean values.
There are other ways to do this. For example you could use a bit array rather than Boolean values to save space. Or you could create a hash table or set in some languages, for example. But the above works, is easy to understand, and can be translated to pretty much any programming language.
My solution with python would be with a random sample
>>> import random
>>>
>>> my_string = "linux-2.6"
>>>
>>> my_set = random.sample(my_string,len(my_string))
>>>
>>> for i in my_set: print i,
2 i . u x l - 6 n
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I need to extract substring. I have found that unlike Python, it is not possible to use indices to extract a substring from a string variable. I have also found that MATLAB does not allow use of negative indices to access elements at end of a vector like Python does.
Looking into MATLAB documentation I have found this function:
https://uk.mathworks.com/help/stateflow/ref/substr.html
However, when I enter this into the MATLAB, I get this:
>> substr
Unrecognized function or variable 'substr'.
I am using MATLAB R2022a. However, this function is not recognized. What could be the reason for this?
Also, are strings in MATLAB closer to the tedious strings of C language and not like the convinient entity they are in Python?
The documentation of substr says:
The operator substr is supported only in Stateflow® charts that use C
as the action language.
so this is not applicable in your case.
It is possible to get substrings of a string using indices. For example, you could use:
test_str = 'thisismyteststring'; % character array
test_str(9:12) % yields 'test'
test_str = "thisismyteststring"; % string array
extractBetween(test_str, 9, 12) % yields "test"
For more information on string handling, check out the documentation here.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm working on a 6809 python processor emulator for educational purposes.
I have a short binary file on the hard drive. The data is raw assembler output for a Motorola MC6809 that has the following contents:
0000300000338cfd4f5f10eec9100012126ec90010adc900186e4c12124cb10255270339121232624f5cf1025527046e4c12126ec4ff00000000
Actual code data:
338cfd4f5f10eec9100012126ec90010adc900186e4c12124cb10255270339121232624f5cf1025527046e4c12126ec4
Using python3.9 how do I get this into a list mem[] as either:
mem[00,00,30,00,00,33,8c,fd,4f,5f,...]
I suspect since ascii characters are involved here quotes would be needed
or
mem[0x00,0x00,0x30,0x00,0x00,0x33,0x8c,0xfd,0x4f,0x5f,...]
all I can seem to do is get the data back as
mem[b'00',b'00',b'30',b'00',b'00',b'33'.b'8c',b'fe',b'4f',b'5f',...]
But I can't seem to typecast these byte values into anything usable.
I've tried a half a dozen methods, with some questionable/unusable results.
Your question implies that you have a file full of ASCII characters, but your self-answer contradicts that by converting single bytes into integers.
It turns out that bytes are already integers. All you need to do is convert the bytes that you read into a list. You could probably work with the bytes object directly, but who am I to judge.
with open("/home/me/Documents/Sources/procproject.BIN", "rb") as memin:
mem = list(memin.read())
thank you for your time and replies
struct is not really a straight up simple method, but I found a rather interesting and simple way to get a byte array into a hex integer array
==========
mem = []
memin = open("/home/me/Documents/Sources/procproject.BIN", "rb")
while True:
mp = bytes(memin.read(1))
if mp == b'':
break
mem.append(hex(int.from_bytes(mp, 'big'))) #this is the magic#
memin.close()
mem=mem[5:]
mem = mem[:-5]
==========
works like a charm
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am new to Haskell. I have a function as:
readData = (map read) . words
I need to match certain constraints and accordingly return data. Is there any way I can do this. Thanks.
I'm going to assume, as seems likely, this function takes a string, splits it into words, and then parses them into integers and you need those integers to be restricted to between 2 and 10,000.
So you can just wrap a check around the list you're already producing. However, to do that you're going to need to change to explicit parameters so that you can talk about them.
readData input = map read (words input)
Now we can filter it
readData input = filter (\n -> n >= 2 && n <= 10000) $ map read (words input)
So any out of range numbers just get dropped from the list.
It's best to remember that while point-free style (not using explicit parameters) can be really nice, it's often not possible to express more complex logic with it. I tend to start out by writing my functions with parameters and then taking them out if I realise I can, and if the resulting code looks easier to understand.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to write a program that searches a string for a version number.
The length of the version may differ and is like this
[number].[number].[....].[number]
for example 1.23.1.
I found something similar in How to extract IP addresses from a text file using Perl?
You should study up on some simple regex expressions, because this isn't that hard of a problem. The regular expression you're searching for might be this:
(\d+\.)+\d+
This will find a string like 1.23.235.22.263
You're supposed to show some effort at solving the problem yourself. But, hey, it's Friday and I'm feeling generous.
[number].[number].[....].[number](e.g. 1.23.1).
This is enough to get us going. By "number" here, I assume that you mean "positive integer". A positive integer is one or more digits. And in regular expressions a digit is matched with \d and we can match "one or more" of something with a +. So "one or more digits" is d+.
So what's our minimal version number. Let's assume it contains two positive integers separated by a dot. We need to be careful here as a dot has a special meaning in regexes (it matches any character). We can turn it into just a dot by escaping it with \. So our minimal version number matches \d+\.\d+.
Now it gets a bit harder. We want to allow any number of \d+\. elements in the middle of our string. A naïve version might be \d+\.(\d+\.)*\d+. The * means "zero or more", so the whole expression means "One or more digits followed by a dot, followed by zero or more repetitions of one or more digits followed by a dot, followed by one or more digits".
But we can improve on that. "One of something" followed by "zero or more of the same thing" is the same as saying "one of more of something". So we can simplify our regex to (\d+\.)+\d+.
Also, as we're going to be capturing the matches, I'd convert that (...) to (?:...) which still groups the regex parts, but doesn't capture the matches.
So we end up with (?:\d+\.)+\d+. Given a string in $string, we can get populate an array with all of the version numbers contained in $string using:
my #versions = $string =~ /((?:\d+\.)+\d+)/g;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
# Change this program to output each letter of the text entered on a separate line
# with space in front of each letter, increasing to the right for each letter.
# For example, 'Python' input should output:
# P
# y
# t
# h
# o
# n
text = input('Enter text: ')
for letter in text:
print(letter)
I already tried to look online for the solution, there are none.
This code is for homework but i cant figure it out help wold be appreciated
As you haven't told us what you have tried to do so far, or what you have tried to learn to figure it out, I'm not going to post the Python code. Instead, lets think about what the program should be doing. Your assignment statement gives a broad overview, but as a programmer you need to take this overview and turn it into a set of smaller instructions. These smaller steps do not have to be in code. They can be in whatever form you like, even plain english.
For functional analysis (which is what you are doing for this problem) start with the inputs and outputs then fill in the stuff in-between.
1) Input: a string
X) Output: multiple lines with a single character and whitespace
Now how do you want to get from 1 to X. You already have the code to loop through each letter and print it, but you are missing two things to get to your required output.
A) way to place a string on a new line
B) way to add whitespace to a string
I'll give you a couple of hints. A) is something that is extremely common in almost any programming language. In fact, it is done the exact same way is any language that you are likely to use. Another hint. Your final output will be a single string that spans multiple lines. Ask yourself how a word processor or text editor gives works with blank lines.
B is a little more tricky, as there are a couple of nifty Python tricks that makes it simpler to do than in other languages. I'm guessing that you already know what happens when you add two numbers together such as 3 + 5. But what happens when you add two strings together such as "foo" + "bar". Notice how the behavior for adding numbers with the + operator is completely different than the behavior is for adding strings together with the same operator? That difference in behavior applies to other common operators as well. Play around with the other three common mathematical operators on both string and numbers to see what happens