在这里完成prolog初学者.
假设我有一个包含食物及其价格的序言知识库,例如:
food(banana,99).
Run Code Online (Sandbox Code Playgroud)
等等
我正在尝试编写一个谓词,如果知识库中有两个或多个具有相同价格的项目,则该谓词将返回true.我遇到的问题是我写的查询:
multiple(X) :- food( _ ,X), food( _ ,X).
Run Code Online (Sandbox Code Playgroud)
如果数据库中只有一个项目的价格为X,则会返回true.我明白问题是什么(它找到相同的项目两次并返回true),但我不明白如何编写一个会找到两个的查询或更多来自食物的独特物品.
我试过写一个"独特的"规则,如下:
multiple(X) :- food(Y,X), food(Z,X), unique(Y,Z).
unique(Y,Z) :- Y /= Z
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.
谢谢.
我想从一个区间生成随机唯一的int数字,但应用程序冻结(无限循环).
int[] ids = new int[200];
for(int i = 0; i < ids.length; i++){
int temp = getUnique(ids);
ids[i] = temp;
}
private int getUnique(int[] ids){
while(true){
int iRand = random(0, ids.length);
if( unique( ids, iRand ) ) return iRand;
}
}
private boolean unique(int[] arr, int i){
for(int k : arr){
if(k == i) return false;
}
return true;
}
private int random(int Min, int Max){
return Min + (int)(Math.random() * ((Max - Min) + 1));
}
Run Code Online (Sandbox Code Playgroud)
我想有一个0到200之间随机排序的整数数组.我不知道为什么,但应用程序正在冻结.为什么?问题出在哪儿?
我需要一个接收数组的函数,并返回一个包含所有重复项的数组.如果可能的话,我更愿意使用下划线.
给定数组:
[
"apple",
"apple",
"pear",
"pear",
"kiwi",
"peach"
]
Run Code Online (Sandbox Code Playgroud)
我需要返回一个数组
[
"apple",
"pear"
]
Run Code Online (Sandbox Code Playgroud)
我发现的许多方法都会返回一个布尔值,而不是一个重复数组.
例如
var fruits = ["apple","apple"];
var uniq_fruits = _.uniq(fruits);
var duplicates_exist = (fruits.length == uniq_fruits.length);
Run Code Online (Sandbox Code Playgroud) static int n = -1;
private static int repeatBuffer[] = new int[10];
static {
repeatBuffer[0] = 0;
//and more
repeatBuffer[9] = 9;
}
static public void randomize() {
do {
Random r = new Random();
randomNumber = r.nextInt(20);
} while (!uniqueInt(randomNumber));
Log.e(TAG, "" + randomNumber); //here I need have a unique int
}
private static Boolean uniqueInt(int random) {
for (int i = 0; i < 9; i++) {
if (random == repeatBuffer[i]) {
return false;
}
}
if (++n …Run Code Online (Sandbox Code Playgroud) 我有这个csv文件
Cat, and, dog, bites
Yahoo, news, claims, a, cat, mated, with, a, dog, and, produced, viable, offspring
Cat, killer, likely, is, a, big, dog
Professional, free, advice, on, dog, training, puppy, training
Cat, and, kitten, training, and, behavior
Dog, &, Cat, provides, dog, training, in Eugene, Oregon
Dog, and, cat, is, a, slang, term, used, by, police, officers, for, a, male-female, relationship
Shop, for, your, show, dog, grooming, and, pet, supplies
Run Code Online (Sandbox Code Playgroud)
我想使所有单词都以小写字母开头,并创建一个列表,其中将包含上述csv文件中的所有唯一项。你有什么主意吗 提前致谢!到目前为止,我已经设法将所有单词转换为一个小字母:
unique_row_items = set([field.strip().lower() for field in …Run Code Online (Sandbox Code Playgroud) 我有一个从数据库中提取州和城市列表的方法.州是独一无二的,但该州可能有许多城市.我的方法目前所做的是将每个州和城市对作为单独的项目返回.我需要它做的是拥有许多城市的州.
目前退货
辛辛那提
哦 - 克利夫兰
哦,findlay
在印第安纳波利斯
我需要它返回
哦,辛辛那提,克利夫兰,芬德利
在印第安纳波利斯
模型
public class Location
{
public string State { get; set; }
public string city { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
REPOSITORY
public HashSet<Location> getlocation()
{
HashSet<Location> myHashset = new HashSet<Location>();
const string storedProc = "someProc";
dynamic locations;
using (var conn = DbFactory.myConnection())
{
locations = conn.Query(storedProc, commandType: CommandType.StoredProcedure);
}
foreach (var location in locations)
{
myHashset.Add(new location{State = location.state,City = location.city});
}
return myHashset
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试查找b / w 2列表的唯一值,但是这种逻辑似乎不起作用
x = [1,2,3,4]
f = [1,11,22,33,44,3,4]
for element in f:
if element in x:
f.remove(element)
print f
Run Code Online (Sandbox Code Playgroud)
期望的输出
[11, 22, 33, 44]
Run Code Online (Sandbox Code Playgroud)
实际产量
[11, 22, 33, 44, 4]
Run Code Online (Sandbox Code Playgroud)
同样在这里问解决方案:
x = [1,2,3,4]
f = [1,11,22,33,44,3,4]
res = list(set(x+f))
print(res)
[1, 2, 3, 4, 33, 11, 44, 22]
Run Code Online (Sandbox Code Playgroud)
如您所见,其添加的1,2,3,4不输出,我需要
给定一个字符串数组,您将如何在数组中找到第一个唯一的String元素
public static String UniqueString(String[] s) {
String str ="";
for(int i=0;i<s.length;i++) {
for(int j=i+1;j<s.length;j++) {
System.out.println(s[i]+" "+s[j]);
str = s[i];
if(str==s[j]) {
break;
}
}if(!(str==s[i+1])){
return str;
}
}
return str;
}
Run Code Online (Sandbox Code Playgroud)
因此{Dog,Cat,Dog,Wolf,lion}的String数组将返回为Cat
我问,因为我喜欢地图不允许多个键。我知道您可以执行以下类似操作,其中您的值是布尔值或空结构,但是有没有办法为您的键指定任何值?必须指定一个空结构有一些好处吗?
相关问题,但只关注附加唯一值。
type N struct {}
func tengoQueCagar() {
var map_almost_empty_value1 = map[int]bool{0:true,1:false}
var map_almost_empty_value2 = map[int]struct{}{0:struct{}{},1:struct{}{}} //long and seems like lame syntax...
var map_almost_empty_value3 = map[int]N{0:N{},1:N{}} //shorter.. better?
var map_not_possible_empty_value_2 = map[int]nil{0:nil,1:nil} // better than empty struct syntax... but not possible
var map_not_possible_empty_value_2 = map[int]{0,1} // ideally possible... but not...
//do something...
}
Run Code Online (Sandbox Code Playgroud)