Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a list of items of a custom data type Film = String String Int, where the strings are the name and director and Int is the year of release.
What's the best way I would go about making a function that outputs a String or set of strings (doesn't matter how long) which show the information like:
Title: (film title) Director: (director) Released: (released)
You need to define a function that creates a String if given a Film as
an input, e.g.:
data Film = Film String String Int
instance Show Film where
show (Film t d r) =
"Title: (" ++ t ++ ") Director: (" ++ d ++ ") Released: (" ++ show(r) ++ ")"
You can read up on type classes and user-defined show here and here.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 months ago.
Improve this question
I'm looking to insert a quote in a string, but keep everything else. So, an example string:
' "2020-10-10",8000,"Hello" '
I want to put quotes around 8000 (or whatever number is there). so:
' "2020-10-10","8000","Hello" '
How would I do that in regex?
I'm not an expert on regex but you can do it you just have to do it twice. Because I couldn't figure out a way to look for ",char or char,".
function test() {
try {
let a = ' "2020-10-10",8000,"Hello" ';
a = a.replace(/,/g,'","');
a = a.replace(/""/g,'"');
console.log(a);
}
catch(err) {
console.log(err);
}
}
7:26:23 AM Notice Execution started
7:26:23 AM Info "2020-10-10","8000","Hello"
7:26:23 AM Notice Execution completed
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Im new to scala so please go easy on me lol.
I need to create a list of where each spot holds an Int,String. So like [(1,"string1"),(2,"String2")...]
for example, Ive tried
val string1 = "something"
val string2 = "something"
List[Int,String] = List[(1,string1), (2,string), (3,string3),(4,string4),(5,string5)]
and I get the error - identifier expected but integer literal found.
How exactly would I get something like this to work?
(1,"string1") is a tuple containing an Int and a String, so type of list should also be a tuple - (Int, String):
val string1 = "something"
val string2 = "something"
// ... rest of string values
val list: List[(Int,String)] = List((1,string1), (2,string2), (3,string3),(4,string4),(5,string5))
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am getting values from an api call and it returns one json value/key pair as a string at a time. I need to count how many times items with a certain prefix (which encodes the type of the item) occur:
Lets say I am getting 'abc123' as the 1st value
def getType(nodeName):
nodeCount = 0
if "abc" in nodeName:
count = count + 1
return "ABC", count
How do I retain this nodeCount value so that next time an item with prefix 'abc' comes in from the api call, the count can be incremented to 2.
Also, I need to create other counters to keep track of the count of other node types, such as 'xyz777'.
I tried to declare nodeCount as global variable but if I add "global count", that will defeat the purpose of retaining the count value for the next api call/iteration.
I am very new to python, so please let me know if there is any easy way.
Many Thanks!
You may use a collections.Counter like this:
from collections import Counter
def getType(counter, nodeName):
nodetype= nodeName.rstrip('0123456789')
counter[nodetype] += 1
return nodetype.upper(), counter[nodetype]
c= Counter()
for n in ['abc123', 'def789', 'ghijk11', 'def99', 'abc444']:
nodetype, nodecount = getType(counter= c, nodeName= n)
print('type {} \t: {}'.format(nodetype, nodecount))
print('summary:')
print(c)
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 4 years ago.
Improve this question
consider:
const int CAP = 20;
struct bookType
{
string bookTitle = "EMPTY";
string ISBN = "EMPTY";
string author = "EMPTY";
string publisher = "EMPTY";
string dateAdded = "EMPTY";
int qty = 0;
double wholesale = 0.00;
double retail = 0.00;
};bookType book[CAP];
What I need to do here is hopefully simple, though I can't seem to get a straight answer on it. I want to search this array of structs (book[]) for a matching bookTitle. for instance, if I have a book named "Star Wars" I need to be able to search the array of structs by typing in "star" and finding a book, "Star Wars". I've been searching for hours, but all the solutions I've found don't seem to actually work.
I don't know the rest of you code so I'll try to give a generic answer.
It seems like you are looking for the find() function for string objects. The find function will return std::string::npos if it does not find anything.
So inside a loop, test:
Booktype[x].bookTitle.find("Star")!=std::string::npos
Change Star to the whatever you are searching for. If this condition is true then you haave a match.
Just a heads up, this is case sensitive so you might want create temporary variables and convert the titles and queries into lowercases and run the loop on them.
Hope this helps.
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 5 years ago.
Improve this question
Hi I'm trying to use list types in haskell.
I have the following types in my .hs file:
type Name = String
type PhoneNumber = Int
type Person = (Name, PhoneNumber)
type PhoneBook = [Person]
I'm looking to add the function
add::Person -> PhoneBook -> PhoneBook
add ........
that adds an entry to the phone book at the beginning of the list. Testing it would be done through the terminal
This is trivially the cons operator (:)
add :: Person -> PhoneBook -> PhoneBook
add = (:)
However I think you're abusing tuples here in Person. You should consider using a custom data type rather than a tuple in most cases. Using record syntax makes life easy for you:
data Person = Person { getName :: Name
, getPhoneNumber :: PhoneNumber }