我需要迭代一组整数的排列.必须通过在每一步交换一对元素来生成订单.
我找到了用于Heap算法的维基百科文章(http://en.wikipedia.org/wiki/Heap%27s_algorithm),它应该这样做.伪代码是:
procedure generate(n : integer, A : array of any):
if n = 1 then
output(A)
else
for i := 1; i ? n; i += 1 do
generate(n - 1, A)
if n is odd then
j ? 1
else
j ? i
swap(A[j], A[n])
Run Code Online (Sandbox Code Playgroud)
我试图在python中为此编写一个生成器:
def heap_perm(A):
n = len(A)
Alist = [el for el in A]
for hp in _heap_perm_(n, Alist):
yield hp
def _heap_perm_(n, A):
if n == 1:
yield A
else:
for …Run Code Online (Sandbox Code Playgroud) 我试图引导时从命令提示符下运行MSTEST 这里.
它工作正常,但我不想每次运行时创建测试结果文件夹或测试结果.我怎样才能做到这一点?
如果它创建了测试结果文件,那么它将整个测试项目DLLS复制到我试图避免的文件夹中.
任何帮助真的很感激.
我刚刚开始使用c#.但无法弄清楚DateTime.我知道它是一个结构,但为什么我会看到不同的方法像类一样初始化它.
如果它是一个结构,这怎么样?
DateTime myValue = DateTime.Now; // This is struct
DateTime myValue2 = new DateTime(); // This is class with +11 overloads.
Run Code Online (Sandbox Code Playgroud)
那么c#one中有两个版本的datetime是struct而另一个是class吗?
我遇到了.NET HttpClient类(.NET 4.5.1,System.Net.Http v4.0.0.0)的问题.我正在调用HttpClient.GetAsync,传入一个CancellationToken(作为Nuget包的一部分,用于抽象webservices之间的调用).如果在进行调用之前已取消令牌,则请求将在不抛出异常的情况下通过.这种行为似乎不正确.
我的测试(不完整,未完全编写 - 无异常检查):
[TestMethod]
public async Task Should_Cancel_If_Cancellation_Token_Called()
{
var endpoint = "nonexistent";
var cancellationTokenSource = new CancellationTokenSource();
var _mockHttpMessageHandler = new MockHttpMessageHandler();
_mockHttpMessageHandler
.When("*")
.Respond(HttpStatusCode.OK);
var _apiClient = new ApiClientService(new HttpClient(_mockHttpMessageHandler));
cancellationTokenSource.Cancel();
var result = await _apiClient.Get<string>(endpoint, null, cancellationTokenSource.Token);
}
Run Code Online (Sandbox Code Playgroud)
我正在测试的方法:
public async Task<T> Get<T>(string endpoint, IEnumerable<KeyValuePair<string, string>> parameters = null, CancellationToken cancellationToken = default(CancellationToken))
{
var builder = new UriBuilder(Properties.Settings.Default.MyEndpointHost + endpoint);
builder.Query = buildQueryStringFromParameters(parameters);
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try …Run Code Online (Sandbox Code Playgroud) 我正在为我的编程类做一个家庭作业,涉及实现接口.这里的问题是我真的只是不了解接口或它们用于什么(教授对解释它不是很好).
分配是制作一个"车辆"超类,而不是三个子类,比如"卡车"或"吉普",每个都有自己的几个特征."车辆"类必须实现类似的界面,我认为我已经想到了(我有compareTo()比较车辆门数的方法),另外一个类也必须实现"混合"类(我不知道)这意味着什么).然后,我们必须实现toString(),equals(Object o)和compareTo(Object o)方法.
我想我已经compareTo()失败了,但equals()我不知道.我们要做的最后一件事是编写一个Main进行测试,这涉及制作一个Vehicle对象数组,打印出来,对它们进行排序,然后重新打印它们.它还应该遍历阵列并打印混合动力车的价格溢价,并使用该equals(Object o)方法来比较2辆车.
这是我的"Vehicle"超类的代码
package vehicle;
abstract public class Vehicle implements Comparable {
private String color;
private int numberOfDoors;
// Constructor
/**
* Creates a vehicle with a color and number of doors
* @param aColor The color of the vehicle
* @param aNumberOfDoors The number of doors
*/
public Vehicle(String aColor, int aNumberOfDoors) {
this.color = aColor;
this.numberOfDoors = aNumberOfDoors;
}
// …Run Code Online (Sandbox Code Playgroud) 我正在使用这个在线编译器玩一些F#代码(我找不到确切的F#编译器版本,这将在以后相关).我意识到以下两个功能非常不同:
let f x y = x = y
let g x y = x != y
Run Code Online (Sandbox Code Playgroud)
f是完全通用的,但令我惊讶的g是没有.调用g 1 2将导致消息的编译错误:
泛型构造要求类型'int'具有引用语义,但它不具有,即它是结构
我去了我自己的机器上尝试过这个(在单声道上使用F#3.0,但我在Visual Studio 2012中得到了相同的结果)并在我尝试定义时收到了完全不同的错误g:
错误FS0332:无法解决在此程序点或附近使用运算符'(!=)'所固有的模糊性.考虑使用类型注释来解决歧义.
为什么这种使用!=含糊不清,为什么会出错呢?另外,为什么这种使用被!=认为是模棱两可的但是使用=in f不是?新错误有助于提醒我一个自动泛化无法按预期工作的情况.但是,这似乎是两个不同版本的F#编译器之间的重大变化.搜索这两个错误消息都显示为空.我没有在F#规范中看到任何看似相关的内容.任何指向相关部分的指针都会有所帮助.
C#的新手,但在PowerShell中经验丰富。接管别人的代码。编写已编译的PowerShell模块,并尝试弄清楚如何基于返回的数据创建对象。现在,代码返回一个字符串:
ServerResponse<UCCSiteModel> requestModel = this.MakeRequest("/site/api/", "site", "GET", this.Credentials, queryString);
StringBuilder builder = new StringBuilder();
if (requestModel != null && requestModel.Response != null)
{
builder.AppendLine("SiteID: " + requestModel.Response.SiteID);
builder.AppendLine("Identity: " + requestModel.Response.SiteName);
builder.AppendLine("Site Code: " + requestModel.Response.SiteCode);
builder.AppendLine("Contact Name: " + requestModel.Response.ContactName);
builder.AppendLine("Contact Number: " + requestModel.Response.ContactNumber);
builder.AppendLine("Contact Email Address: " + requestModel.Response.ContactEmailAddress);
builder.AppendLine("Address: " + requestModel.Response.Address);
builder.AppendLine("City: " + requestModel.Response.City);
builder.AppendLine("State: " + requestModel.Response.State);
builder.AppendLine("Post Code: " + requestModel.Response.PostCode);
builder.AppendLine("Time Zone: " + requestModel.Response.Timezone);
builder.AppendLine("Longitude: " + requestModel.Response.longitude);
builder.AppendLine("Latitude: " …Run Code Online (Sandbox Code Playgroud) 我遇到了自定义依赖项属性绑定的问题.我们有:自定义用户控件,具有一个依赖属性并绑定到self:
<UserControl x:Class="WpfApplication1.SomeUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Grid>
<Label>
<Label.Template>
<ControlTemplate>
<Label Content="{Binding MyTest}"/>
</ControlTemplate>
</Label.Template>
</Label>
</Grid>
Run Code Online (Sandbox Code Playgroud)
......和控制代码:
public partial class SomeUserControl : UserControl
{
public SomeUserControl()
{
InitializeComponent();
}
public static readonly DependencyProperty MyTestProperty = DependencyProperty.Register("MyTest", typeof(int), typeof(SomeUserControl));
public int MyTest
{
get { return (int)GetValue(MyTestProperty); }
set { SetValue(MyTestProperty, value); }
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用此控件绑定到简单模型类的一些简单属性:
<UserControl x:Class="WpfApplication1.AnotherUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:wpfApplication1="clr-namespace:WpfApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/> …Run Code Online (Sandbox Code Playgroud) 我遇到了多线程的绊脚石.我想我知道问题是什么,但无法确定如何解决它.但我可能错了.
总之,我有生产者和消费者线程.生产者线程将来自外部源的数据收集到数据表中,然后将它们放入集合中.然后,消费者从集合中获取数据表.我使用BlockingCollection作为公共静态集合,因此两个线程都可以访问它,它存在于两个不同的类中.我现在将展示代码的主要部分,然后解释什么是和不起作用.
制片人主题:
try
{
dataTable.Clear();
adapter.Fill(dataTable);
dataCaptured = true;
timeout = 0;
ThreadInfo.setCurrentDate(startDate);
ThreadInfo.dataTableCollection.Add(dataTable);
}
Run Code Online (Sandbox Code Playgroud)
消费者线程
while(true)
{
DataTable testTable = ThreadInfo.dataTableCollection.Take();
foreach (DataRow datarow in testTable.Rows)
{
foreach (var item in datarow.ItemArray)
{
Console.WriteLine(item);
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以我的测试显示,当生产者线程创建数据表时,它成功地将它们添加到集合中.我可以通过在add方法之前和之后使用count来看到这一点.计算每个表中的行数我还可以确认添加的表是与创建的表相同.此外,take方法还成功删除了一个表,该表与输入的表匹配.我知道这一点既可以计算集合中的表数,也可以计算'take'数据表中的行数.
我的问题是当我尝试运行foreach循环以打印出结果时.最初它工作并开始将数据打印到屏幕,但随后抛出此错误:
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Collection was modified; enumeration operation might not execute.
Source=System.Data
StackTrace:
at System.Data.RBTree`1.RBTreeEnumerator.MoveNext()
at pullPlexTable.InputThreads.dataConsumerThread() in \\srv-file01\users$\dkb\Visual Studio 2013\Projects\pullPlexTable\pullPlexTable\InputThread.cs:line 39
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback …Run Code Online (Sandbox Code Playgroud) 我想要一本事件词典,到目前为止我都有
private Dictionary<T, event Action> dictionaryOfEvents;
Run Code Online (Sandbox Code Playgroud)
有可能做这样的事吗?
c# ×7
.net ×1
algorithm ×1
cancellation ×1
comparable ×1
datetime ×1
dependencies ×1
events ×1
f# ×1
generics ×1
inheritance ×1
interface ×1
java ×1
mstest ×1
object ×1
permutation ×1
powershell ×1
properties ×1
python ×1
superclass ×1
unit-testing ×1
wpf ×1