Prolog, display list result, after algorithm - recursion

I got a list of names, and according to whom owns what, I want to split it in 2 different list. I debugged my code and it seems to work, but the console is always returning true, true true...
Expected result: LA = [jean, julie, marc], LB = [jean, julie].
I'd guess my stopped condition is not working correctly, but i'm not sure why.
variables are basically : Head-Now(HN), Rest-Now(RN), List-Automobile(LA), List-Bicycle(LB)
owns(julie, car, red).
owns(jean, car, red).
owns(jean, car, green).
owns(marc, car, red).
owns(jean, bicycle, blue).
owns(nathalie, car, black).
owns(julie, bicycle, white).
owns(benjo, car, white).
%divide list of names into list of names owning car, and another list owning bicycle
% What to ask in console : setProp([jean, julie, arthur, marc], LA, LB).
setProp([], LA, LB).
setProp([HN|RN], LA, LB):- owns(HN,car,_), owns(HN,bicycle,_), setProp(RN, [HN|LA], [HN|LB]).
setProp([HN|RN], LA, LB):- owns(HN,car,_), setProp(RN, [HN|LA], LB).
setProp([HN|RN], LA, LB):- owns(HN,bicycle,_), setProp(RN, LA,[HN|LB]).
setProp([HN|RN], LA, LB):- setProp(RN, LA,LB).

You have your rules "backwards". The correct form is the one below:
setProp([], [], []).
setProp([HN|RN], [HN|LA], [HN|LB]):- owns(HN,car,_), owns(HN,bicycle,_), setProp(RN, LA, LB).
setProp([HN|RN], [HN|LA], LB):- owns(HN,car,_), setProp(RN, LA, LB).
setProp([HN|RN], LA, [HN|LB]):- owns(HN,bicycle,_), setProp(RN, LA, LB).
setProp([_|RN], LA, LB):- setProp(RN, LA,LB).
This set of rules gives you the expected answer.

Related

What is wrong with my R codes for removing line breaks in a column?

I have a dataframe called "df00" and it has the first column named as "Room type". This column has a lot of line breaks in it.
I need to make a copy of this dataframe (let's call it df00_dup) and add a duplicate of the first column in this new dataframe. The duplicated column will be named "RoomInfo". I need to remove the line breaks found in the column "RoomInfo".
Here is an extract of the first column of df00:
Room type
King Room with Balcony...
King Room with Balcony...
Deluxe Room...
Deluxe Room...
Column RoomInfo in df00_dup is, of course, the same as above.
My R codes to remove those line breaks currently stand as follows:
df00_dup <- df00
df00_dup$RoomInfo <- gsub("[\r\n]", "", df00[1])
When I run the second line from the above codes, the column RoomInfo now displays the following:
RoomInfo
c("King Room with Balcony\n\n\n\n\n\n\n\n\n\n\n...
c("King Room with Balcony\n\n\n\n\n\n\n\n\n\n\n...
c("King Room with Balcony\n\n\n\n\n\n\n\n\n\n\n...
c("King Room with Balcony\n\n\n\n\n\n\n\n\n\n\n...
All rows of the RoomInfo column is now filled with the above.
Expected output:
RoomInfo
King Room with Balcony\n\n\n\n\n\n\n\n\n\n\n...
King Room with Balcony\n\n\n\n\n\n\n\n\n\n\n...
Deluxe Room\n\n\n\n\n\n\n\n\n\n\n...
Deluxe Room\n\n\n\n\n\n\n\n\n\n\n...
What is wrong with my codes? How can I remove those line breaks by maintaining the correct mapping?
Added (dput):
structure(list(`Room type` = c("King Room with Balcony\n\n\n\n\n\n\n\n\n\n2 large double beds\n\n\n\n\n\n\n\n\n50 m²BalconyGarden viewPrivate bathroom\n\n\n\nFree toiletries\n\n\n\n\nToilet\n\n\n\n\nFireplace\n\n\n\n\nBath or shower\n\n\n\n\nTowels\n\n\n\n\nLinen\n\n\n\n\nSocket near the bed\n\n\n\n\nDesk\n\n\n\n\nSeating Area\n\n\n\n\nSlippers\n\n\n\n\nMosquito net\n\n\n\n\nIroning facilities\n\n\n\n\nTea/Coffee maker\n\n\n\n\nOutdoor furniture\n\n\n\n\nOutdoor dining area\n\n\n\n\nWake-up service\n\n\n\n\nEntire unit located on ground floor\n\n\n\n\nClothes rack\n\n\n\n\nDrying rack for clothing\n\n\n\n\nToilet paper\n\n\n\n\nHand sanitiser",
"King Room with Balcony\n\n\n\n\n\n\n\n\n\n2 large double beds\n\n\n\n\n\n\n\n\n50 m²BalconyGarden viewPrivate bathroom\n\n\n\nFree toiletries\n\n\n\n\nToilet\n\n\n\n\nFireplace\n\n\n\n\nBath or shower\n\n\n\n\nTowels\n\n\n\n\nLinen\n\n\n\n\nSocket near the bed\n\n\n\n\nDesk\n\n\n\n\nSeating Area\n\n\n\n\nSlippers\n\n\n\n\nMosquito net\n\n\n\n\nIroning facilities\n\n\n\n\nTea/Coffee maker\n\n\n\n\nOutdoor furniture\n\n\n\n\nOutdoor dining area\n\n\n\n\nWake-up service\n\n\n\n\nEntire unit located on ground floor\n\n\n\n\nClothes rack\n\n\n\n\nDrying rack for clothing\n\n\n\n\nToilet paper\n\n\n\n\nHand sanitiser",
"Deluxe Family Room\n\n\n\n\n\n\n\n\n\n2 extra-large double beds\n\n\n\n\n\n\n\n\n70 m²BalconyGarden viewPrivate bathroom\n\n\n\nFree toiletries\n\n\n\n\nToilet\n\n\n\n\nSofa\n\n\n\n\nFireplace\n\n\n\n\nBath or shower\n\n\n\n\nTowels\n\n\n\n\nLinen\n\n\n\n\nSocket near the bed\n\n\n\n\nDesk\n\n\n\n\nSeating Area\n\n\n\n\nSlippers\n\n\n\n\nMosquito net\n\n\n\n\nIroning facilities\n\n\n\n\nTea/Coffee maker\n\n\n\n\nOutdoor furniture\n\n\n\n\nOutdoor dining area\n\n\n\n\nWake-up service\n\n\n\n\nEntire unit located on ground floor\n\n\n\n\nClothes rack\n\n\n\n\nDrying rack for clothing\n\n\n\n\nToilet paper\n\n\n\n\nHand sanitiser\n\n\n\nMore",
"Deluxe Family Room\n\n\n\n\n\n\n\n\n\n2 extra-large double beds\n\n\n\n\n\n\n\n\n70 m²BalconyGarden viewPrivate bathroom\n\n\n\nFree toiletries\n\n\n\n\nToilet\n\n\n\n\nSofa\n\n\n\n\nFireplace\n\n\n\n\nBath or shower\n\n\n\n\nTowels\n\n\n\n\nLinen\n\n\n\n\nSocket near the bed\n\n\n\n\nDesk\n\n\n\n\nSeating Area\n\n\n\n\nSlippers\n\n\n\n\nMosquito net\n\n\n\n\nIroning facilities\n\n\n\n\nTea/Coffee maker\n\n\n\n\nOutdoor furniture\n\n\n\n\nOutdoor dining area\n\n\n\n\nWake-up service\n\n\n\n\nEntire unit located on ground floor\n\n\n\n\nClothes rack\n\n\n\n\nDrying rack for clothing\n\n\n\n\nToilet paper\n\n\n\n\nHand sanitiser\n\n\n\nMore",
"Deluxe Family Room\n\n\n\n\n\n\n\n\n\n2 extra-large double beds\n\n\n\n\n\n\n\n\n70 m²BalconyGarden viewPrivate bathroom\n\n\n\nFree toiletries\n\n\n\n\nToilet\n\n\n\n\nSofa\n\n\n\n\nFireplace\n\n\n\n\nBath or shower\n\n\n\n\nTowels\n\n\n\n\nLinen\n\n\n\n\nSocket near the bed\n\n\n\n\nDesk\n\n\n\n\nSeating Area\n\n\n\n\nSlippers\n\n\n\n\nMosquito net\n\n\n\n\nIroning facilities\n\n\n\n\nTea/Coffee maker\n\n\n\n\nOutdoor furniture\n\n\n\n\nOutdoor dining area\n\n\n\n\nWake-up service\n\n\n\n\nEntire unit located on ground floor\n\n\n\n\nClothes rack\n\n\n\n\nDrying rack for clothing\n\n\n\n\nToilet paper\n\n\n\n\nHand sanitiser\n\n\n\nMore"
), `Price for 2 nights` = c("MUR 46,748\n\n\n\nPrice\nMUR 46,748\n\n\n\n\n+MUR 4,030 taxes and charges",
"MUR 23,374\n\n\n\nPrice\nMUR 23,374\n\n\n\n\n+MUR 2,015 taxes and charges",
"MUR 59,334\n\n\n\nPrice\nMUR 59,334\n\n\n\n\n+MUR 5,115 taxes and charges",
"MUR 89,001\n\n\n\nPrice\nMUR 89,001\n\n\n\n\n+MUR 7,672 taxes and charges",
"MUR 29,667\n\n\n\nPrice\nMUR 29,667\n\n\n\n\n+MUR 2,557 taxes and charges"
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))
We would need to extract the column as a vector as df00[1] would still be a data.frame with a single column and gsub/sub expects a vector as input
df00_dup$RoomInfo <- gsub("[\r\n]", "", df00[[1]])

How sort vertices by edge values in Gremlin

Given the air-routes graph, say I want to get all possible one-stopover routes, like so:
[home] --distance--> [stopover] --distance--> [destination]
where [home], [stopover] and [destination] are airport nodes that each have a property 'code' that represent an airport code; and distance is an integer weight given to each edge connecting two airport nodes.
How could I write a query that gets me the airport codes for [home], [stopover] and [destination] such that the results are sorted as follows:
[home] airport codes are sorted alphabetically.
For each group of [home] airport, the [stopover] airport codes are sorted by the distance between [home] and [stopover] (ascending).
After sorting 1 and 2, [destination] airport codes are sorted by the distance between [stopover] and [destination].
(Note: it doesn't matter if [home] and [destination] are the same airport)
One way you could do this is through group with nested by modulation.
g.V().
group().
by('code').
by(
outE('route').
order().by('dist').
inV().
group().
by('code').
by(
outE('route').
order().by('dist').
inV().
values('code').fold())).
unfold()
The result is something like:
1. {'SHH': {'WAA': ['KTS', 'SHH', 'OME'], 'OME': ['TLA', 'WMO', 'KTS', 'GLV', 'ELI', 'TNC', 'WAA', 'WBB', 'SHH', 'SKK', 'KKA', 'UNK', 'SVA', 'OTZ', 'GAM', 'ANC']}}
2. {'KWN': {'BET': ['WNA', 'KWT', 'ATT', 'KUK', 'TLT', 'EEK', 'WTL', 'KKH', 'KWN', 'KLG', 'MLL', 'KWK', 'PQS', 'CYF', 'KPN', 'NME', 'OOK', 'GNU', 'VAK', 'SCM', 'HPB', 'EMK', 'ANC'], 'EEK': ['KWN', 'BET'], 'TOG': ['KWN']}}
3. {'NUI': {'SCC': ['NUI', 'BTI', 'BRW', 'FAI', 'ANC'], 'BRW': ['ATK', 'AIN', 'NUI', 'PIZ', 'SCC', 'FAI', 'ANC']}}
4. {'PSG': {'JNU': ['HNH', 'GST', 'HNS', 'SGY', 'SIT', 'KAE', 'PSG', 'YAK', 'KTN', 'ANC', 'SEA'], 'WRG': ['PSG', 'KTN']}}
5. {'PIP': {'UGB': ['PTH']}}
.
.
.

Trying to Filter dataframe based on strings in a column R

The dataset is a list of injuries, my index is a series of words found in some of the injuries. I'd like to filter out all of the injuries in that column that do not contain any of the words from the index.
Here is what I'm starting with:
x
index
torn meniscus
torn
sprained ankle
broken
broken leg
pulled hamstring
This is what I'd like to have, based on matching the index with the column:
x
torn meniscus
broken leg
As far as code goes, I'm stumped at how to include the whole list without typing out every index word to compare the x column against. I have:
df %>% select(Date, Team, Injury, Players) %>%
filter(str_detect(Injury, ))
I'd love to be able to do this in Excel, but unfortunately Excel doesn't take too kindly to 20,000+ rows being filtered with an index as large as the one I need.
Here is a more representative sample of my data, along with the full index. I removed one word from the index because of several cases of a minor injury description containing the word.
Injury
"placed on IL with torn meniscus in left knee (out for season)"
"placed on IL with sore right knee"
"placed on IL with left foot injury (out for season)"
"placed on IL with strained left hamstring (out for season)"
"returned to lineup"
"returned to lineup"
"activated from IL"
"placed on IL with right knee injury / conditioning"
"placed on IL with sprained right ankle"
"dislocated kneecap in left knee (out indefinitely)"
"activated from IL"
"placed on IL with sore right knee"
"placed on IL with sprained left ankle"
"placed on IL for personal reasons (out for season)"
"placed on IL with left leg injury"
"placed on IL with sore right knee"
"placed on IL with right calf injury"
"placed on IL with neck spasms"
"placed on IL with right groin injury"
"activated from IL"
"placed on IL with right hip injury"
"placed on IL with sore right knee"
"placed on IL with bruised right quadriceps"
"placed on IL with dislocated kneecap in left knee (out for season)"
"activated from IL"
"placed on IL with left calf injury (out for season)"
"placed on IL with strained left hamstring (out for season)"
"placed on IL"
"activated from IL"
"right knee injury (out for season)"
"placed on IL with bruised lower left leg"
"activated from IL"
"surgery on left knee to repair dislocated kneecap (out for season)"
Index
[1] "fracture" "broken" "break" "tear" "torn" "ligament"
[7] "tendon" "ACL" "MCL" "meniscus" "rupture" "surgery"
[13] "bone" "hernia" "tendinitis" "chronic" "dislocate" "seperate"
You could concatenate a complex regular expression from the unique values un column df$index.
library(tidyverse)
df <- tibble(
x = c("placed on IL with torn meniscus in left knee (out for season)", "placed on IL with sore right knee", "placed on IL with left foot injury (out for season)", "placed on IL with strained left hamstring (out for season)", "returned to lineup", "returned to lineup", "activated from IL", "placed on IL with right knee injury / conditioning", "placed on IL with sprained right ankle", "dislocated kneecap in left knee (out indefinitely)", "activated from IL", "placed on IL with sore right knee", "placed on IL with sprained left ankle", "placed on IL for personal reasons (out for season)", "placed on IL with left leg injury", "placed on IL with sore right knee", "placed on IL with right calf injury", "placed on IL with neck spasms", "placed on IL with right groin injury", "activated from IL", "placed on IL with right hip injury", "placed on IL with sore right knee", "placed on IL with bruised right quadriceps", "placed on IL with dislocated kneecap in left knee (out for season)", "activated from IL", "placed on IL with left calf injury (out for season)", "placed on IL with strained left hamstring (out for season)", "placed on IL", "activated from IL", "right knee injury (out for season)", "placed on IL with bruised lower left leg", "activated from IL", "surgery on left knee to repair dislocated kneecap (out for season)"),
index = c("fracture", "broken", "break", "tear", "torn", "ligament", "tendon", "ACL", "MCL", "meniscus", "rupture", "surgery", "bone", "hernia", "tendinitis", "chronic", "dislocate", "seperate", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA))
df %>%
## In the next line we take all values from `index`, remove NA,
## and append into a regex with the OR symbol ('|') separating strings.
filter(grepl(pattern = unique(na.omit(df$index)) %>% paste0(collapse = "|"),
x = x)) %>%
select(-index) ## Here you remove the column index from the data frame
Results:
# A tibble: 4 x 1
x
<chr>
1 placed on IL with torn meniscus in left knee (out for season)
2 dislocated kneecap in left knee (out indefinitely)
3 placed on IL with dislocated kneecap in left knee (out for season)
4 surgery on left knee to repair dislocated kneecap (out for season)
What you are looking for is text matching, the grep function in base R.
data = c("placed on IL with torn meniscus in left knee (out for season)", "placed on IL with sore right knee", "placed on IL with left foot injury (out for season)", "placed on IL with strained left hamstring (out for season)", "returned to lineup", "returned to lineup", "activated from IL", "placed on IL with right knee injury / conditioning", "placed on IL with sprained right ankle", "dislocated kneecap in left knee (out indefinitely)", "activated from IL", "placed on IL with sore right knee", "placed on IL with sprained left ankle", "placed on IL for personal reasons (out for season)", "placed on IL with left leg injury", "placed on IL with sore right knee", "placed on IL with right calf injury", "placed on IL with neck spasms", "placed on IL with right groin injury", "activated from IL", "placed on IL with right hip injury", "placed on IL with sore right knee", "placed on IL with bruised right quadriceps", "placed on IL with dislocated kneecap in left knee (out for season)", "activated from IL", "placed on IL with left calf injury (out for season)", "placed on IL with strained left hamstring (out for season)", "placed on IL", "activated from IL", "right knee injury (out for season)", "placed on IL with bruised lower left leg", "activated from IL", "surgery on left knee to repair dislocated kneecap (out for season)")
index = c("fracture", "broken", "break", "tear", "torn", "ligament", "tendon", "ACL", "MCL", "meniscus", "rupture", "surgery", "bone", "hernia", "tendinitis", "chronic", "dislocate", "separate")
pattern = paste0(index, collapse="|")
result = grep(pattern, data, value=TRUE, invert=TRUE)
head(result)
# [1] "placed on IL with sore right knee"
# [2] "placed on IL with left foot injury (out for season)"
# [3] "placed on IL with strained left hamstring (out for season)"
# [4] "returned to lineup"
# [5] "returned to lineup"
# [6] "activated from IL"
Explanation
You want to find elements from data that do not contain any string from the index vector.
We construct regular expression from the index vector that would do this match. This is done with the | symbol. Pattern A|B matches elements that contain either A or B. So we just connect the elements of index vector with the | symbol:
pattern = paste0(index, collapse="|")
Now, we will match this pattern with grep tell it to return the matched values:
grep(pattern, data) # returns indices
grep(pattern, data, value=TRUE) # return matched items
Now, you do not want matched items, but items that do not match. This can be simply done with the invert argument:
grep(pattern, data, value=TRUE, invert=TRUE)
This will return items that did not match the regex pattern.

Is There A Neat/Simplest Way To This data.table R Code?

The STRATUM from OECD data is so long, for simplicity I put this name and would like to simplified it to a more short and precise naming as in the code below.
pisaMas[,`:=`
(SchoolType = c(ifelse(STRATUM == "National Secondary School", "Public",
ifelse(STRATUM == "Religious School", "Religious",
ifelse(STRATUM == "MOE Technical School", "Technical",0)))))]
pisaMas[,table(SchoolType)]
I would like to know if there are a simple way to this problems, using data.table package.
Current development version of data.table has new function fcase (modeled after SQL CASE WHEN) for this situation:
pisaMas[ , SchoolType := fcase(
STRATUM == "National Secondary School", "Public",
STRATUM == "Religious School", "Religious",
STRATUM == "MOE Technical School", "Technical",
default = ''
)]
pisaMas[ , table(SchoolType)]
To get the development version, try
install.packages(
'data.table', type = 'source',repos = 'http://Rdatatable.github.io/data.table'
)
If the simple install doesn't work, you can check the Installation wiki for some more details:
https://github.com/Rdatatable/data.table/wiki/Installation
You can also solve this with a lookup table, see this Q&A for details:
https://stackoverflow.com/a/36391018/3576984
This is what I come out with after a few thoughts.
#' First I create a function (rname.SchType) that have oldname and newname using else if:
rname.SchType <- function(x){
if (is.na(x)) NA
else if (x == "MYS - stratum 01: MOE National Secondary School\\Other States")"Public"
else if(x == "MYS - stratum 02: MOE Religious School\\Other States")"Religious"
else if(x == "MYS - stratum 03: MOE Technical School\\Other States")"Technical"
else if(x == "MYS - stratum 04: MOE Fully Residential School")"SBP"
else if(x == "MYS - stratum 05: non-MOE MARA Junior Science College\\Other States")"MARA"
else if(x == "MYS - stratum 06: non-MOE Other Schools\\Other States")"Private"
else if(x == "MYS - stratum 07: Perlis non-“MOE Fully Residential Schools”")"Perlis Fully Residential"
else if(x == "MYS - stratum 08: Wilayah Persekutuan Putrajaya non-“MOE Fully Residential Schools”")"Putrajaya Fully Residential"
else if(x == "MYS - stratum 09: Wilayah Persekutuan Labuan non-“MOE Fully Residential Schools”")"Labuan Fully Residential"
}
By using the function I just created, I past it through the data.table with just a line of code, by applying base R (sapply) inside data.table, hence managed to avoid code clutter-ness and look much simpler:
pisaMalaysia[,`:=`(jenisSekolah = sapply(STRATUM,rname.SchType))]
I think I finally got the answer for my question above! This answer overcome the issue of 'not vectorized' as mentioned by #Roland, thank you sir! And it is in my opinion is much faster even though it took me literally couple of weeks to understand the concept and finding the right questions on the web!
First, I create a new data.table that consist of 2 columns, one with the original name and the second is the desired name for the school.
lookUpStratum <- data.table(STRATUM=c("MYS - stratum 01: MOE National Secondary School\\Other States",
"MYS - stratum 02: MOE Religious School\\Other States",
"MYS - stratum 03: MOE Technical School\\Other States",
"MYS - stratum 04: MOE Fully Residential School",
"MYS - stratum 05: non-MOE MARA Junior Science College\\Other States",
"MYS - stratum 06: non-MOE Other Schools\\Other States",
"MYS - stratum 07: Perlis non-“MOE Fully Residential Schools”",
"MYS - stratum 08: Wilayah Persekutuan Putrajaya non-“MOE Fully Residential Schools”",
"MYS - stratum 09: Wilayah Persekutuan Labuan non-“MOE Fully Residential Schools”"),
SCH.TYPE=c("Public",
"Religious",
"Technical",
"SBP",
"MARA",
"Private",
"Perlis Fully Residential",
"Putrajaya Fully Residential",
"Labuan Fully Residential"))
The answer lies on the setDT (Coerce lists and data.frames to data.table by reference).
Using this line of code I read here, it looks kinda long but it solved my problem! And to be honest I understand this first before I understand the shortest one code below.
setDT(pisaMalaysia)[,SCH.TYPE := lookUpStratum$SCH.TYPE[match(pisaMalaysia$STRATUM,lookUpStratum$STRATUM)]]
After a few minutes I finally managed to get my head around this code here and produced this code:
setDT(pisaMalaysia)[lookUpStratum,SCH.TYPE1 := i.SCH.TYPE, on = c(STRATUM = "STRATUM")]
I got these answers from the same post here.
To check if everything is the same:
table(pisaMalaysia$SCH.TYPE)
table(pisaMalaysia$SCH.TYPE1)
#' original data
pisaMalaysia[,table(STRATUM)]
results:
> table(pisaMalaysia$SCH.TYPE)
Labuan Fully Residential MARA Perlis Fully Residential
54 122 78
Private Public Putrajaya Fully Residential
385 4929 78
Religious SBP Technical
273 2661 281
> table(pisaMalaysia$SCH.TYPE1)
Labuan Fully Residential MARA Perlis Fully Residential
54 122 78
Private Public Putrajaya Fully Residential
385 4929 78
Religious SBP Technical
273 2661 281
> pisaMalaysia[,table(STRATUM)]
STRATUM
MYS - stratum 01: MOE National Secondary School\\Other States
4929
MYS - stratum 02: MOE Religious School\\Other States
273
MYS - stratum 03: MOE Technical School\\Other States
281
MYS - stratum 04: MOE Fully Residential School
2661
MYS - stratum 05: non-MOE MARA Junior Science College\\Other States
122
MYS - stratum 06: non-MOE Other Schools\\Other States
385
MYS - stratum 07: Perlis non-“MOE Fully Residential Schools”
78
MYS - stratum 08: Wilayah Persekutuan Putrajaya non-“MOE Fully Residential Schools”
78
MYS - stratum 09: Wilayah Persekutuan Labuan non-“MOE Fully Residential Schools”
54
Thanks! Hope this will help others too.

product collection from sub-categories id from object manager where subcategory was created in custom category which located out of root category

I want all product of particular category on basis of category ID.
I have already tried below code but not solved:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cateinstance = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
$allcategoryproduct = $cateinstance->create()->load($cateid)->getProductCollection()->addAttributeToSelect('*');
[{"entity_id":"245","sku":"U001","type_id":"simple","category_id":null,"cat_index_position":"0","product_image":"\/p\/e\/peach-boy-boys-shirts_1_a8c05a1421e5dcf8ad5308398e0b3591_1.jpg","small_image":"\/p\/e\/peach-boy-boys-shirts_1_a8c05a1421e5dcf8ad5308398e0b3591_1.jpg","thumbnail":"\/p\/e\/peach-boy-boys-shirts_1_a8c05a1421e5dcf8ad5308398e0b3591_1.jpg","productName":"DY Andheri Peach School Uniform","price":"500.0000","isProductInStock":19,"isProductEndorsed":false,"option_collection":[]},{"entity_id":"250","sku":"U003","type_id":"configurable","category_id":null,"cat_index_position":"0","product_image":"\/0\/a\/0aea950b-1c0a-4adf-85ce-c521555462651534842479667-chalk-by-pantaloons-girls-dresses-4851534842479574-1.jpg","small_image":"\/0\/a\/0aea950b-1c0a-4adf-85ce-c521555462651534842479667-chalk-by-pantaloons-girls-dresses-4851534842479574-1.jpg","thumbnail":"\/0\/a\/0aea950b-1c0a-4adf-85ce-c521555462651534842479667-chalk-by-pantaloons-girls-dresses-4851534842479574-1.jpg","productName":"DY Andheri Girls School Uniform","price":"450.0000","isProductInStock":0,"isProductEndorsed":false,"option_collection":[{"sku":"DY Andheri Girls School Uniform-S","product_id":"250","attribute_id":"151","default_title":"Size","value_index":"30","display_label":"S","price":"450.0000"},{"sku":"DY Andheri Girls School Uniform-M","product_id":"250","attribute_id":"151","default_title":"Size","value_index":"31","display_label":"M","price":"450.0000"},{"sku":"DY Andheri Girls School Uniform-L","product_id":"250","attribute_id":"151","default_title":"Size","value_index":"32","display_label":"L","price":"450.0000"},{"sku":"DY Andheri Girls School Uniform-XL","product_id":"250","attribute_id":"151","default_title":"Size","value_index":"33","display_label":"XL","price":"450.0000"}]}]
You can get specific category product collection by
$categoryId = 3;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
$category = $categoryFactory->create()->load($categoryId);
$productCollectionFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addCategoryFilter($category);
$collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
You can replace categoryId with your category Id and collection will return all product which is enabled and visibility is set to catalog search and that belong to specify category

Resources